将图像缩放为缩略图大小并加载到图像视图中以节省内存的问题
我试图将图像缩小到更小的尺寸,我将它们加载到图像视图中,然后我开始收到“位图大小超出虚拟机预算”异常。另一方面,如果我获得缩略图而不是实际图像并向它们显示,则运行会很顺利。但我需要实际图像的 URI 供以后使用,如果我立即加载缩略图,则无法获得这些 URI。
查了一下,找到了一个办法,但是不行。
代码:
Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, name);
uriList.add(uri.getPath());
File image = new File(uri.getPath());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap actualBitmap = BitmapFactory.decodeFile(image.getPath(), options);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(actualBitmap, image_width, image_height, false);
iv.setImageBitmap(scaledBitmap);
我获取actualBitmap的部分返回null,因此imageview是空的。如果我打印 uri.getpath() ,它会给出:
/external/images/media//sdcard/dcim/Camera/imagename.jpg
我的问题是,这是正确的方法吗?如果是,我做错了什么,如果不是,请有人指出我正确的方向。
I am trying to scale my images down to smaller size I was loading them as they were into an imageview and I started getting a "bitmap size exceeds VM budget" exception. On the other hand if I get the thumbnails instead of the actual images and show them it runs smooth. But I need the URIs of the actual images for later use which I can't get if I straight away load the thumbnails.
After looking around, I found a way, but it's not working.
Code:
Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, name);
uriList.add(uri.getPath());
File image = new File(uri.getPath());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap actualBitmap = BitmapFactory.decodeFile(image.getPath(), options);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(actualBitmap, image_width, image_height, false);
iv.setImageBitmap(scaledBitmap);
The part where I get the actualBitmap returns null, and so the imageview is empty. If I print uri.getpath()
it gives:
/external/images/media//sdcard/dcim/Camera/imagename.jpg
My question is, is this the right approach? If it is, what am I doing wrong and if it's not can someone please point me in the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用以下代码
try with the following code