how to compress image for imageview in android(如何在android中为imageview压缩图像)
问题描述
您好,我想在视图中显示 3 或 4 张存储在 sdcard 中的图像,图像大小约为 1-2 MB.我的问题是当我在 imageview 中使用图像时,它会抛出内存异常将图像解码为位图时,我有创建位和传递选项
Hi I want to show 3 or 4 image in my view that are stored in sdcard the size of images is 1-2 MB approximately. My problem is when I use image in imageview then it throw out of memory exception i have create bit and pass option when decoding image to bitmap
02-26 13:16:54.946: ERROR/dalvikvm-heap(23410): 15980544-byte external allocation too large for this process.
02-26 13:16:54.946: ERROR/dalvikvm(23410): Out of memory: Heap Size=3407KB, Allocated=2801KB, Bitmap Size=15630KB, Limit=21884KB
02-26 13:16:54.946: ERROR/dalvikvm(23410): Trim info: Footprint=3463KB, Allowed Footprint=3655KB, Trimmed=248KB
02-26 13:16:54.946: ERROR/GraphicsJNI(23410): VM won't let us allocate 15980544 bytes
02-26 13:16:54.986: ERROR/AndroidRuntime(23410): FATAL EXCEPTION: main
02-26 13:16:54.986: ERROR/AndroidRuntime(23410): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-26 13:16:54.986: ERROR/AndroidRuntime(23410): at android.graphics.BitmapFactory.nativeDecodeFile(Native Method)
02-26 13:16:54.986: ERROR/AndroidRuntime(23410): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:325)
任何机构都可以帮助我解决我的问题吗提前致谢
can any body help me in solving my problem thanks in advance
推荐答案
我找到了我的问题的解决方案有我的代码:
I have found solution of my problem there is my code :
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[24*1024];
options.inJustDecodeBounds = false;
options.inSampleSize=32;
bmp1=BitmapFactory.decodeFile(filepath1,options);
Bitmap b1=ThumbnailUtils.extractThumbnail(bmp1,30, 30);
iv1.setImageBitmap(b);
if(bmp1!=null){
bmp1.recycle();
}
bmp1=BitmapFactory.decodeFile(filepath1,options);
Bitmap b2=ThumbnailUtils.extractThumbnail(bmp1,30, 30);
iv2.setImageBitmap(b2);
if(bmp1!=null){
bmp1.recycle();
}
类似地,我将它用于四个图像视图并设置图像而没有 OOM 异常
similarly I have use it for four image view and set image without OOM Exception
这篇关于如何在android中为imageview压缩图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在android中为imageview压缩图像
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01