I am developing a coloring book. And I want the state of the drawing to be saved when the user exits the application or changes the screen orientation.
My method works correctly when I set it to a button.
But when I add it to onPause it saves only part of the image.
I have already added execution of it in a separate thread, but this does not help either.
Here is my method:
private void setSavedBitmap(Bitmap finalBitmap) {
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"savedBitmap1.png");
new Thread(new Runnable() {
public void run() {
try {
FileOutputStream fos = null;
fos = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
if (fos != null) fos.close();
} catch (Exception e) {
e.printStackTrace();
};
}
}).start();
SharedPreferencesFactory.saveString(this, "savedBitmap1", file.getPath());
}
onPause:
@Override
protected void onPause() {
super.onPause();
setSaveColor();
setSavedBitmap(colourImageView.getmBitmap());
}
on button:
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setSavedBitmap(colourImageView.getmBitmap());
Toast.makeText(ColoringActivity.this, "You state image was saved",
Toast.LENGTH_LONG).show();
}
});
UPD.
If I close the application and go to Device File Explorer to open my bitmap file, I see the following message:
I don't understand why this method works fine for a button but doesn't work in onPause()
It also turned out that if you change the format from PNG to JPEG, the method works fine even in onPause(). But I only need PNG