[英]How to save and restore bitmap before close app
I wanna make replaceable personal background for app's users.我想为应用程序的用户制作可替换的个人背景。 When they change picture I can't save it before close app.当他们更改图片时,我无法在关闭应用程序之前保存它。 I try shared preferences but not working for bitmap.我尝试共享首选项但不适用于位图。 How can I save and restore bitmap before close app?如何在关闭应用程序之前保存和恢复位图?
//use this method to save your bitmap, call this method when you have bitmap
private void saveBitmap(Bitmap pBitmap){
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File directory = contextWrapper.getDir("folderName", Context.MODE_PRIVATE);
if (!directory.exists()) {
directory.mkdirs();
}
File file = new File(directory, "fileName.png");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
pBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
String filePath = file.getAbsolutePath();
//save this path in shared preference to use in future.
} catch (Exception e) {
Log.e("SAVE_IMAGE", e.getMessage(), e);
}
}
use this method to get bitmap from file path that you saved使用此方法从您保存的文件路径中获取位图
private void getBitmapFromPath(String pFilePath) {
try {
File f = new File(pFilePath);
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(f));
//use this bitmap as you want
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
for saving and retrieving file path用于保存和检索文件路径
//This for saving file path
PreferenceManager.getDefaultSharedPreferences(context).edit().putString("FILE_PATH_KEY", filePath).apply();
//this for getting saved file path
String filePath = PreferenceManager.getDefaultSharedPreferences(context).getString("FILE_PATH_KEY", "path not retrieved successfully!");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.