private void destoryBimap() { if (photo != null && !photo.isRecycled()) { photo.recycle(); photo = null; } }
/** * 读取路径中的图片,然后将其转化为缩放后的bitmap * @param path */ public String saveBefore(String path) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 获取这个图片的宽和高 Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回bm为空 options.inJustDecodeBounds = false; // 计算缩放比 int be = (int) (options.outHeight / (float) 200); if (be <= 0) be = 1; options.inSampleSize = 4; // 图片长宽各缩小至四分之一 // 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false哦 bitmap = BitmapFactory.decodeFile(path, options); // savePNG_After(bitmap,path); return saveJPGE_After(bitmap, path); } /** * 保存图片为JPEG * @param bitmap * @param path */ public String saveJPGE_After(Bitmap bitmap, String path) { File file = new File(path); try { FileOutputStream out = new FileOutputStream(file); if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) { out.flush(); out.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return path; }