如何在关闭应用之前保存和恢复位图

huangapple go评论76阅读模式
英文:

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?

答案1

得分: 1

// 使用这个方法来保存位图,在你有位图时调用此方法
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();
        // 在将来使用时,将这个路径保存在共享偏好中。
    } catch (Exception e) {
        Log.e("SAVE_IMAGE", e.getMessage(), e);
    }
}

// 使用这个方法从文件路径获取位图(你之前保存的)
private void getBitmapFromPath(String pFilePath) {
    try {
        File f = new File(pFilePath);
        Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(f));
        // 根据需要使用这个位图
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

// 用于保存和检索文件路径的部分

// 保存文件路径的部分
PreferenceManager.getDefaultSharedPreferences(context).edit().putString("FILE_PATH_KEY", filePath).apply();

// 获取已保存的文件路径的部分
String filePath = PreferenceManager.getDefaultSharedPreferences(context).getString("FILE_PATH_KEY", "未成功检索到路径!");
英文:
//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!");

huangapple
  • 本文由 发表于 2020年9月30日 01:50:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64124973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定