public class ViewHolder { public ImageView image; } Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 1: adapter.notifyDataSetChanged(); break; } super.handleMessage(msg); } }; public void loading() { new Thread(new Runnable() { public void run() { while (true) { if (Bimp.max == Bimp.tempSelectBitmap.size()) { Message message = new Message(); message.what = 1; handler.sendMessage(message); break; } else { Bimp.max += 1; Message message = new Message(); message.what = 1; handler.sendMessage(message); } } } }).start(); } } public String getString(String s) { String path = null; if (s == null) return ""; for (int i = s.length() - 1; i > 0; i++) { s.charAt(i); } return path; } protected void onRestart() { adapter.update(); super.onRestart(); } private static final int TAKE_PICTURE = 0x000001; public void photo() { // Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // startActivityForResult(openCameraIntent, TAKE_PICTURE); // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/myimage/", String.valueOf(System // .currentTimeMillis()) + ".jpg"); // camePath = file.getPath(); // Uri imageUri = Uri.fromFile(file); // intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); // startActivityForResult(intent, TAKE_PICTURE); if (hasSdcard()) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//��������� intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile)); // intent.putExtra(MediaStore.EXTRA_OUTPUT, cutImgUri); startActivityForResult(intent, PHOTO_REQUEST_CAMERA); } // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File( // Environment.getExternalStorageDirectory(), "androidapp.jpg"))); // ((Activity) mContext).startActivityForResult(intent, 2); } // public void photo() { // Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // startActivityForResult(openCameraIntent, TAKE_PICTURE); //// Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //// File file = new File(Environment.getExternalStorageDirectory() + "/myimage/", String.valueOf(System //// .currentTimeMillis()) + ".jpg"); //// camePath = file.getPath(); //// Uri imageUri = Uri.fromFile(file); //// intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); //// startActivityForResult(intent, TAKE_PICTURE); // } // protected void onActivityResult(int requestCode, int resultCode, Intent data) { // System.out.println("--------data------->"+data); // //���ѡȡ // if(requestCode == PHOTO_REQUEST_CAMERA && resultCode == Activity.RESULT_OK ){ // //��������ͼƬ�ͽ�ȡ���ͼƬ�ļ���д����ͬһ���ļ���photo.jpg // crop(Uri.fromFile(tempFile),Uri.fromFile(tempFile)); // } // //���ؽ�ȡ��ͼƬ // else if(requestCode == PHOTO_REQUEST_CUT ){ // //���д�ͼʹ��Uri // Bitmap bitmap = decodeUriAsBitmap(Uri.fromFile(tempFile));//decode bitmap //// mFace.setImageBitmap(bitmap); // String fileName = String.valueOf(System.currentTimeMillis()); // ImageItem takePhoto = new ImageItem(); // takePhoto.setBitmap(bitmap); // takePhoto.setImagePath(FileUtils.SDPATH + fileName + ".JPEG"); // Bimp.tempSelectBitmap.add(takePhoto); // }else { //// Toast.makeText(getApplicationContext(), "��ȡͼƬʧ��", Toast.LENGTH_SHORT).show(); // } // super.onActivityResult(requestCode, resultCode, data); // if (resultCode != Activity.RESULT_OK) { // return; // } // switch (requestCode) { // // 如果是直接从相册获取 // case REQUSET: // poi_jing = data.getStringExtra("poi_jing"); // Log.e("返回==poi_jing",poi_jing); // poi_wei = data.getStringExtra("poi_wei"); // Log.e("返回==poi_wei",poi_wei); // et_share_address.setText(data.getStringExtra("address")); // default: // break; // } // } // protected void onActivityResult(int requestCode, int resultCode, Intent data) { // switch (requestCode) { // case TAKE_PICTURE: // if (Bimp.tempSelectBitmap.size() < 3 && resultCode == RESULT_OK) { // //// String fileName = String.valueOf(System.currentTimeMillis()); //// Bundle bundle = data.getExtras("data"); //// Bitmap bm = (Bitmap) data.getExtras().get("data"); //// FileUtils.saveBitmap(bm, fileName); //// //// ImageItem takePhoto = new ImageItem(); //// takePhoto.setBitmap(bm); //// takePhoto.setImagePath(FileUtils.SDPATH + fileName + ".JPEG"); //// Bimp.tempSelectBitmap.add(takePhoto); // //// SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS"); // // Bitmap bitmap = BitmapFactory.decodeFile(camePath); // File file = new File(String.valueOf(Environment.getDataDirectory())); // file.mkdirs(); // String fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myimage/" + System.currentTimeMillis() // + ".jpg"; // try { // FileOutputStream b = new FileOutputStream(fileName); // bitmap.compress(Bitmap.CompressFormat.JPEG, 50, b); // b.flush(); // b.close(); // bitmap = Bimp.revitionImageSize(fileName); // FileUtils.saveBitmap(bitmap, // "" + fileName.substring(fileName.lastIndexOf("/") + 1, fileName.lastIndexOf("."))); // ImageItem takePhoto = new ImageItem(); // takePhoto.setBitmap(bitmap); // takePhoto.setImagePath(fileName); // Bimp.tempSelectBitmap.add(takePhoto); // if (null != adapter) { // adapter.notifyDataSetChanged(); // } // File refile = new File(camePath); // if (refile.exists()) { // refile.delete(); // } // } catch (Exception e) { // e.printStackTrace(); // } // // //// Bitmap bitmap = BitmapFactory.decodeFile(camePath); //// File file = new File(Environment.getExternalStorageDirectory() + "/myImage/"); //// file.mkdirs(); //// String fileName = Environment.getExternalStorageDirectory() + "/myImage/" + System.currentTimeMillis() //// + ".jpg"; //// try { //// FileOutputStream b = new FileOutputStream(fileName); //// bitmap.compress(Bitmap.CompressFormat.JPEG, 50, b); //// b.flush(); //// b.close(); //// bitmap = Bimp.revitionImageSize(fileName); //// FileUtils.saveBitmap(bitmap, //// "" + fileName.substring(fileName.lastIndexOf("/") + 1, fileName.lastIndexOf("."))); //// ImageItem takePhoto = new ImageItem(); //// takePhoto.setBitmap(bitmap); //// takePhoto.setImagePath(fileName); //// Bimp.tempSelectBitmap.add(takePhoto); //// if (null != adapter) { //// adapter.notifyDataSetChanged(); //// } //// File refile = new File(camePath); //// if (refile.exists()) { //// refile.delete(); //// } //// } catch (Exception e) { //// e.printStackTrace(); //// } // } // // break; //// case 1: //// if (data != null) { //// startPhotoZoom(data.getData()); //// Toast.makeText(mContext, "拖动正方形角可调整剪切大小", //// android.widget.Toast.LENGTH_SHORT).show(); //// } //// break; //// // 如果是调用相机拍照时 //// case 2: //// File temp = new File(Environment.getExternalStorageDirectory() //// + "/androidapp.jpg"); //// startPhotoZoom(Uri.fromFile(temp)); //// break; // } // } // private void cameraCamera(Intent data) { // SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS"); // String name = formatter.format(System.currentTimeMillis()) + ".jpg"; // Log.i("zhiwei.zhao", "image name:" + name); // Toast.makeText(this, name, Toast.LENGTH_LONG).show(); // Bundle bundle = data.getExtras(); // /* 获取相机返回的数据,并转换为Bitmap图片格式 */ // Bitmap bitmap = (Bitmap) bundle.get("data"); // FileOutputStream b = null; // // String path = Environment.getExternalStorageDirectory().getPath(); // File file = new File(path + "/myImage/"); // /** 检测文件夹是否存在,不存在则创建文件夹 **/ // if (!file.exists() && !file.isDirectory()) // file.mkdirs(); // String fileName = file.getPath() + "/" + name; // Log.i("zhiwei.zhao", "camera file path:" + fileName); // try { // b = new FileOutputStream(fileName); // /* 把数据写入文件 */ // bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } finally { // try { // if (b == null) // return; // b.flush(); // b.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // showImgs(bitmap, true); // } // public class MyLocationListenner implements BDLocationListener { // // // @Override // public void onReceiveLocation(BDLocation location) { // if (location == null || mMapView == null) // return; // MyLocationData locData = new MyLocationData.Builder() // .accuracy(location.getRadius()).direction(1000) // .latitude(location.getLatitude()) // .longitude(location.getLongitude()).build(); // mBaiduMap.setMyLocationData(locData); // if (isFirstLoc) { // isFirstLoc = false; // mLocClient.stop(); // mBaiduMap.setMyLocationEnabled(true); // LatLng ll = new LatLng(location.getLatitude(), // location.getLongitude()); // if (TextUtils.isEmpty(location.getCity())) { //// mSearch.reverseGeoCode(new ReverseGeoCodeOption() //// .location(ll)); // } else { // city = location.getCity(); // } // MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll); // mBaiduMap.animateMapStatus(u); // getPoint(); // } // } // // public void onReceivePoi(BDLocation poiLocation) { // } // } @Override protected void onDestroy() { Bimp.tempSelectBitmap.clear(); Bimp.max = 0; super.onDestroy(); } private Bitmap compressBmpFromBmp(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int options = 100; image.compress(Bitmap.CompressFormat.JPEG, 60, baos); while (baos.toByteArray().length / 1024 > 200) { baos.reset(); options -= 10; image.compress(Bitmap.CompressFormat.JPEG, options, baos); } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null); return bitmap; } private void crop(Uri uri, Uri cutImgUri) { // �ü�ͼƬ��ͼ Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); // �ü���ı�����1��1 // intent.putExtra("aspectX", 4); // intent.putExtra("aspectY", 3); // �ü������ͼƬ�ijߴ��С // intent.putExtra("outputX", 400); // intent.putExtra("outputY", 300); // ͼƬ��ʽ intent.putExtra("outputFormat", "JPEG"); intent.putExtra("noFaceDetection", true);// ȡ������ʶ�� intent.putExtra("return-data", false);// true:������uri��false������uri intent.putExtra(MediaStore.EXTRA_OUTPUT, cutImgUri);//д���ȡ��ͼƬ startActivityForResult(intent, PHOTO_REQUEST_CUT); } private boolean hasSdcard() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { return true; } else { return false; } } private static String getPath(String path) { File f = new File(path); if (!f.exists()) { f.mkdirs(); } return f.getAbsolutePath(); } private File getFile(String path) { File f = new File(path); if (!f.exists()) { try { f.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } return f; } public Bitmap decodeUriAsBitmap(Uri uri) { Bitmap bitmap = null; try { bitmap = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri)); } catch (FileNotFoundException e) { e.printStackTrace(); return null; } return bitmap; } private Bitmap comp(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 30, baos); if (baos.toByteArray().length / 1024 > 1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出 baos.reset();//重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//这里压缩50%,把压缩后的数据存放到baos中 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); BitmapFactory.Options newOpts = new BitmapFactory.Options(); //开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; //现在主流手机比较多是800*480分辨率,所以高和宽我们设置为 float hh = 800f;//这里设置高度为800f float ww = 480f;//这里设置宽度为480f //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;//be=1表示不缩放 if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;//设置缩放比例 newOpts.inPreferredConfig = Bitmap.Config.RGB_565;//降低图片从ARGB888到RGB565 //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 isBm = new ByteArrayInputStream(baos.toByteArray()); bitmap = BitmapFactory.decodeStream(isBm, null, newOpts); return compressImage(bitmap);//压缩好比例大小后再进行质量压缩 } private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 80, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 int options = 70; while (baos.toByteArray().length / 1024 > 300) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩 baos.reset();//重置baos即清空baos options -= 10;//每次都减少10 image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片 return bitmap; } private Bitmap imageZoom(Bitmap bm) { // 图片允许最大空间 单位:KB double maxSize = 200.00; // 将bitmap放至数组中,意在bitmap的大小(与实际读取的原文件要大) ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] b = baos.toByteArray(); // 将字节换成KB double mid = b.length / 1024; // 判断bitmap占用空间是否大于允许最大空间 如果大于则压缩 小于则不压缩 if (mid > maxSize) { // 获取bitmap大小 是允许最大大小的多少倍 double i = mid / maxSize; // 开始压缩 此处用到平方根 将宽带和高度压缩掉对应的平方根倍 // (1.保持刻度和高度和原bitmap比率一致,压缩后也达到了最大大小占用空间的大小) bm = zoomImage(bm, bm.getWidth() / Math.sqrt(i), bm.getHeight() / Math.sqrt(i)); } return bm; } public Bitmap zoomImage(Bitmap bgimage, double newWidth, double newHeight) { // 获取这个图片的宽和高 float width = bgimage.getWidth(); float height = bgimage.getHeight(); // 创建操作图片用的matrix对象 Matrix matrix = new Matrix(); // 计算宽高缩放率 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 缩放图片动作 matrix.postScale(scaleWidth, scaleHeight); Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width, (int) height, matrix, true); // iv_2.setImageBitmap(bitmap); // tv_2.setText(bitmap.getRowBytes() * bitmap.getHeight() + ""); return bitmap; } @Override protected void onResume() { super.onResume(); // SharedPreferencesUtil sharedPreferencesUtil = new SharedPreferencesUtil(getApplication()); // sharedPreferencesUtil.setFileName("shareInfo"); // String address =sharedPreferencesUtil.getString("address", ""); // String costway =sharedPreferencesUtil.getString("costWay", ""); // jing =sharedPreferencesUtil.getString("poi_jing", ""); // wei =sharedPreferencesUtil.getString("poi_wei", ""); //// String tel =sharedPreferencesUtil.getString("tel", ""); ////// String car =sharedPreferencesUtil.getString("car", ""); //// String alipay =sharedPreferencesUtil.getString("alipay", ""); //// carType.setText(car); // et_share_address.setText(address); // costWay.setText(costway); } public Bitmap getHttpBitmap(String url) { Bitmap bitmap = null; try { URL pictureUrl = new URL(url); InputStream in = pictureUrl.openStream(); bitmap = BitmapFactory.decodeStream(in); in.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bitmap; } public void savePicture(Bitmap bitmap) { String pictureName = "/storage/emulated/0/DCIM/Camera/aaaa.jpg"; File file = new File(pictureName); FileOutputStream out; try { out = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuItem item = menu.add(Menu.NONE, Menu.NONE, Menu.NONE, "Exit"); item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { System.exit(0); return true; } }); return true; } } Anmelden - Gogs: Go Git Service

Anmelden