/** 压缩图片 */ public static Bitmap compressImageFromFile(String srcPath) { Options newOpts = new Options(); newOpts.inJustDecodeBounds = true; // 只读边,不读内容 Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; float hh = 800f; // float ww = 480f; // int 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 = Config.ARGB_8888; // 该模式是默认的,可不设 newOpts.inPurgeable = true; // 同时设置才会有效 newOpts.inInputShareable = true; // 当系统内存不够时候图片自动被回收 bitmap = BitmapFactory.decodeFile(srcPath, newOpts); // return compressBmpFromBmp(bitmap);//原来的方法调用了这个方法企图进行二次压缩 // 其实是无效的,大家尽管尝试 return bitmap; }
private void copyOptions(Options srcOptions, Options destOptions) { destOptions.inDensity = srcOptions.inDensity; destOptions.inDither = srcOptions.inDither; destOptions.inInputShareable = srcOptions.inInputShareable; destOptions.inJustDecodeBounds = srcOptions.inJustDecodeBounds; destOptions.inPreferredConfig = srcOptions.inPreferredConfig; destOptions.inPurgeable = srcOptions.inPurgeable; destOptions.inSampleSize = srcOptions.inSampleSize; destOptions.inScaled = srcOptions.inScaled; destOptions.inScreenDensity = srcOptions.inScreenDensity; destOptions.inTargetDensity = srcOptions.inTargetDensity; destOptions.inTempStorage = srcOptions.inTempStorage; if (Build.VERSION.SDK_INT >= 10) copyOptions10(srcOptions, destOptions); if (Build.VERSION.SDK_INT >= 11) copyOptions11(srcOptions, destOptions); }
public Bitmap loadCachedResource(CachedResource resource) { try { Options options = new Options(); options.inPurgeable = true; options.inInputShareable = true; return BitmapFactory.decodeStream( new FlushedInputStream(new FileInputStream(new File(resource.getPath())))); } catch (Exception e) { if (resource != null) { Log.e("MyTag", "Error decoding image : " + resource.getPath(), e); } System.gc(); return null; } }
public Builder() { decodingOptions.inPurgeable = true; decodingOptions.inInputShareable = true; }