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();