Android异步加载全解析之Bitmap

public static OutputStream decodeBitmap(String path) {BitmapFactory.Options opts = new BitmapFactory.Options();opts.inJustDecodeBounds = true;// 设置成了true,不占用内存,只获取bitmap宽高BitmapFactory.decodeFile(path, opts);opts.inSampleSize = computeSampleSize(opts, -1, 1024 * 800);opts.inJustDecodeBounds = false;// 这里一定要将其设置回false,因为之前我们将其设置成了trueopts.inPurgeable = true;opts.inInputShareable = true;opts.inDither = false;opts.inPurgeable = true;opts.inTempStorage = new byte[16 * 1024];FileInputStream is = null;Bitmap bmp = null;InputStream ins = null;ByteArrayOutputStream baos = null;try {is = new FileInputStream(path);bmp = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts);double scale = getScaling(opts.outWidth * opts.outHeight, 1024 * 600);Bitmap bmp2 = Bitmap.createScaledBitmap(bmp,(int) (opts.outWidth * scale),(int) (opts.outHeight * scale), true);bmp.recycle();baos = new ByteArrayOutputStream();bmp2.compress(Bitmap.CompressFormat.JPEG, 100, baos);bmp2.recycle();return baos;} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {is.close();ins.close();baos.close();} catch (IOException e) {e.printStackTrace();}System.gc();}return baos;} private static double getScaling(int src, int des) {/** * 目标尺寸÷原尺寸 sqrt开方,得出宽高百分比 */double scale = Math.sqrt((double) des / (double) src);return scale;}

,人生没有停靠站,自我本身永远是一个出发点。

Android异步加载全解析之Bitmap

相关文章:

你感兴趣的文章:

标签云: