0

I am trying to fetch bitmap from file and what I get is out of memory exception, here is the code, I have tried everything - bitmap.recycle, bitmap == null, setting largeHeap to true in Manifest - but the error still happens. The error happens at bitmap = BitmapFactory.decodeStream(is); The solution should work from jelly bean android versions till the newest one. Here is the code:

mMemoryStorage.mRetriever = new MediaMetadataRetriever();
try {
    for (int i = 0; i < mMemoryStorage.AllsongsArray.size(); i++) {
        FileInputStream inputStream = new FileInputStream(mMemoryStorage.AllsongsArray.get(i).mPath);
        mMemoryStorage.mRetriever.setDataSource(inputStream.getFD());
        inputStream.close();

        byte[] art = mMemoryStorage.mRetriever.getEmbeddedPicture();
        Bitmap bitmap = null;
        if (art != null) {

            InputStream is = new ByteArrayInputStream(art);
            *Error//////bitmap = BitmapFactory.decodeStream(is);////////////Error*

            final int maxSize = 512;
            int outWidth;
            int outHeight;
            int inWidth = bitmap.getWidth();
            int inHeight = bitmap.getHeight();
            if (inWidth > inHeight) {
                outWidth = maxSize;
                outHeight = (inHeight * maxSize) / inWidth;
            }
            else {
                outHeight = maxSize;
                outWidth = (inWidth * maxSize) / inHeight;
            }
            //CAUSES OUT OF MEMORY ERROR
            Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, outWidth, outHeight, false);
            bitmap.recycle();
            bitmap = null;
            mMemoryStorage.AllsongsArray.get(i).mBitmapEmbedded = resizedBitmap;
        }
    }
} catch (Exception ex) {
    ex.printStackTrace();
}
mMemoryStorage.mRetriever.release();
Anton Potapov
  • 1,265
  • 8
  • 11
Karlsson Makhno
  • 101
  • 2
  • 10
  • Edit in Manifest Add Also see https://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object?rq=1 – Mukesh M Aug 05 '17 at 18:55
  • Oh, I haven't mentioned it - this doesn't work either. – Karlsson Makhno Aug 05 '17 at 19:07
  • @MM Don't do that. Unless you have a really unique app, its a hack. Fix the actual problem instead. – Gabe Sechan Aug 05 '17 at 19:08
  • @KarlssonMakhno Your app in general is using too much memory. The first question to ask is if this is a huge image. If it is, that may be the problem (images take about 4*width*height bytes of memory). If not, you need to take heap dumps and see where your app is using memory and either reduce it or fix any leaks. Make sure you're using row recycling for lists and caches for images. – Gabe Sechan Aug 05 '17 at 19:10
  • Ok, I will try this, thanks. – Karlsson Makhno Aug 05 '17 at 19:14
  • 1
    @GabeSechan BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = SampleSize; BitmapFactory.decodeFile(f.getPath(), options); – Mukesh M Aug 05 '17 at 19:20
  • which sample size to choose? – Karlsson Makhno Aug 05 '17 at 19:21
  • SampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels.Any value <= 1 is treated the same as 1. Note: the decoder uses a final value based on powers of 2, any other value will be rounded down to the nearest power of 2. – Mukesh M Aug 05 '17 at 19:27
  • But the quality will be pretty low then – Karlsson Makhno Aug 05 '17 at 19:28
  • choose inSampleSize that works for you – Mukesh M Aug 05 '17 at 19:29
  • 1
    Check Android Monitor in android studio to see RAM usage of your app – Mukesh M Aug 05 '17 at 19:30
  • @KarlssonMakhno try 2 or choose inSampleSize that works for you – Mukesh M Aug 05 '17 at 19:36

0 Answers0