external allocation too large for this process

…衆ロ難τιáo~ 提交于 2019-11-29 11:06:21
slayton

The error you are getting means that the bitmap you are loading is too big. You need to downsample it. See this question for some great examples on how to deal with this: Strange out of memory issue while loading an image to a Bitmap object

Update: Instead of telling the ImageView which URI to load as a Bitmap, you need to load the bitmap first yourself and then set that bitmap as the image on the ImageView. This is pretty straightforward.

To load a bitmap from you must first convert the URI to a String (I think you can use URI.getPath() although I'm not sure) that contains the path to the image then you can load the bitmap with:

Bitmap b = BitmapFactory.decodeFile(pathToImage);

This will decode the entire bitmap, instead you want to downsample the bitmap to the desired resolution. Lets say your imageview will be 125x125 pixels and your image is 1000x1000 pixels. 125/1000 = 8 so we only need to decode every 8th pixel. To do this we do the following:

int inSample = 8;

opts = new BitmapFactory.Options();
opts.inSampleSize = inSample;

Bitmap b = BitmapFactory.decodeFile(pathToImage, opts); // this bitmap will be 1/8 the size of the original

Its pretty straight forward and should solve your memory issues. See the 2nd part of my answer to this question: Android GalleryView Recycling for a more extensive explanation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!