external allocation too large for this process

后端 未结 1 1871
时光说笑
时光说笑 2020-12-19 11:44

I posted a question last night about this issue, but I dont think I explained it well enough to get the proper help. So basically, I have an application where you can press

相关标签:
1条回答
  • 2020-12-19 12:04

    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.

    0 讨论(0)
提交回复
热议问题