I have an app to create collages with a feature to apply filters on individual photos. To keep the app memory efficient and fast while applying filters, I simply store a copy of original photo(cropped version) in the Context, using Context.OpenFileOutput() which is used to apply filters realtime.
So, every time a filter is applied to a photo, I simply rewrite the mutable bitmap with the one saved in Context using OpenFileInput() and then apply the filter on it. This saves me from keeping two copies of same bitmap in memory.
Here's the problem I am facing which does not occur in debug mode on my devices (Nexus 5 and Samsung Galaxy Grand) but I have many crashes(caught ones) in the crashlytics.
According to the logs in crashlytics, the file does not exist in the Context, but actually it does exist.
Here's my code:
public static Bitmap decodeFileToBitmap(Context context,
String filename,Bitmap bmp)
throws IOException{
try {
FileInputStream fis = context.getApplicationContext().openFileInput(filename);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
options.inSampleSize = 1;
options.inBitmap = bmp;
Bitmap bitmap = BitmapFactory.decodeStream(fis, null, options);
fis.close();
return bitmap;
}catch (Exception ex){
return null;
}
}
public static void deleteFileWithName(Context context, String filename){
try {
context.deleteFile(filename);
}catch (Exception ex){}
}
/**/
public static void saveFileInternally(Context context,
String filename,Bitmap bmp) throws IOException{
FileOutputStream fos = context.getApplicationContext().openFileOutput(filename,Context.MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
}
Here's the raw log:
Non-fatal Exception: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Posix.java)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at libcore.io.IoBridge.open(IoBridge.java:393)
at java.io.FileInputStream.<init>(FileInputStream.java:78)
at android.app.ContextImpl.openFileInput(ContextImpl.java:959)
at android.content.ContextWrapper.openFileInput(ContextWrapper.java:186)
at com.photos.storage.SnapAuraExternalStorage.decodeFileToBitmap(SnapAuraExternalStorage.java:38)
at com.instacollage.managers.PhotoFilterManager$1.run(PhotoFilterManager.java:70)
at java.lang.Thread.run(Thread.java:841)
Well, I didn't see any Non-Fatals or Exceptions while calling Context.openFileOutput().