I'm trying to get the simple decodeFile
working and I'm loosing my mind.
The below code returns null
while the logcat says: D/skia﹕ --- decoder->decode returned false
Aldo sometimes it manages to work (one out of mby 50 times).
try {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inDither = true;
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
canvasBitmap = BitmapFactory.decodeFile(myFile.getAbsolutePath(), opt);
} catch (OutOfMemoryError e) {
Log.d(TAG, "Trace: " + e);
System.gc();
try {
Log.d(TAG, "Trying again");
canvasBitmap = BitmapFactory.decodeFile(myFile.getAbsolutePath());
} catch (OutOfMemoryError e2) {
Log.d(TAG, "Trace: " + e2);
Log.d(TAG, "Out of memory!!!!!!!");
}
}
The File is defined and getAbsolutePath returns /storage/emulated/0/Pictures/Screenshots/Screenshot_2015-07-28-16-18-56.png
The File is basically a screenshot taken on the phone, it can be opened just fine on gallery apps and pc so it's not corrupt.
If for some reason this just isn't gonna work is there any custom libraries with their own decoders? I've tried a couple libraries but they all seem to use the same decodeFile.
I'm testing on nexus 4 so the image is 760p.
I've solved this by adding a while
loop with an if else
The following code:
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inDither = true;
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
int i = 0;
while (canvasBitmap == null && ++i < 99) {
System.gc();
Log.d(TAG, "Trying again: " + i);
canvasBitmap = BitmapFactory.decodeFile(myFile.getAbsolutePath(), opt);
}
It seems to be quite inefficient but works, I'm also running it outside main thread so it's not gonna cause any not responsive behavior.