使用bitmap缩略图,解决图片大小超过预算的VM的问题
16lz
2021-01-24
Android 2.3.3 Eclipse Version: 3.7.0 LogCat
Activity中显示大图,部分源代码如下:
try {bitmap = BitmapFactory.decodeFile("/" + nokia.getBitmap(), opt);} catch (Exception e) {e.printStackTrace();}nokiaImage.setImageBitmap(bitmap);
使用过程中
LogCat 报错信息:
02-07 13:14:22.947: DEBUG/dalvikvm(33246): GC_EXTERNAL_ALLOC freed 75K, 61% free 3080K/7751K, external 14227K/16275K, paused 100ms02-07 13:14:23.017: ERROR/dalvikvm-heap(33246): 4147200-byte external allocation too large for this process.02-07 13:14:23.137: ERROR/GraphicsJNI(33246): VM won't let us allocate 4147200 bytes02-07 13:14:23.148: DEBUG/dalvikvm(33246): GC_FOR_MALLOC freed 2K, 61% free 3078K/7751K, external 14227K/16275K, paused 37ms02-07 13:14:23.191: DEBUG/skia(33246): --- decoder->decode returned false02-07 13:14:23.191: DEBUG/AndroidRuntime(33246): Shutting down VM02-07 13:14:23.191: WARN/dalvikvm(33246): threadid=1: thread exiting with uncaught exception (group=0x40015560)02-07 13:14:23.218: ERROR/AndroidRuntime(33246): FATAL EXCEPTION: main02-07 13:14:23.218: ERROR/AndroidRuntime(33246): java.lang.OutOfMemoryError: bitmap size exceeds VM budget02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:284)02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at com.tmall.nokia.Book.handerUI(Book.java:522)02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at com.tmall.nokia.Book$1.run(Book.java:396)02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at android.os.Handler.handleCallback(Handler.java:587)02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at android.os.Handler.dispatchMessage(Handler.java:92)02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at android.os.Looper.loop(Looper.java:123)02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at android.app.ActivityThread.main(ActivityThread.java:3683)02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at java.lang.reflect.Method.invokeNative(Native Method)02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at java.lang.reflect.Method.invoke(Method.java:507)02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at dalvik.system.NativeStart.main(Native Method)02-07 13:14:23.268: WARN/ActivityManager(61): Force finishing activity com.tmall.nokia/.Book
发生错误的原因分析:
主要是bitmap图片尺寸较大,使用时超过预算的VM;
网上有两种解决办法:
一种是增加VM大小
VMRuntime.getRuntime().setMinimumHeapSize(12 * 1024 * 1024); VMRuntime.getRuntime().setTargetHeapUtilization(0.85f);
经个人测试无效;
另一种是使用缩略图,缩小图片的尺寸大小
//图片缩小倍数opt.inSampleSize = 2;
可以实现。
更多相关文章
- Android(安卓)特殊用法--来自中国移动开发社区
- Android(安卓)之 Bitmap 和 File 相互转换
- android camera Intent调用
- android按屏幕大小动态确定控件位置及大小
- android中实现从一个图片中截取一部分,在翻转,成一个圆形显示
- Android(安卓)学习笔记-2011年7月
- android-------开发常用框架汇总
- Android(安卓)SwitchButton(滑动开关)
- Android(安卓)DownloadManager 使用