Android 保存自定义数组包括位图

Android save custom Array include bitmap

我尝试保存包含 String 和 ArrayList 的 ArrayList
我的 saveData 和 loadData 出错,因为它与位图不兼容。
我需要一些建议或解决我的问题,谢谢。

错误

A/Bitmap: Error, cannot access an invalid/free'd bitmap here!
A/.scnapplicatio: runtime.cc:655] Runtime aborting...
runtime.cc:655] All threads:
runtime.cc:655] DALVIK THREADS (19):
runtime.cc:655] "main" prio=10 tid=1 Runnable
runtime.cc:655]   | group="" sCount=0 dsCount=0 flags=0 obj=0x7157cc28 self=0xe0400e10
runtime.cc:655]   | sysTid=6883 nice=-10 cgrp=top-app sched=0/0 handle=0xeedc0478
runtime.cc:655]   | state=R schedstat=( 773111122 159407281 346 ) utm=53 stm=23 core=2 HZ=100
runtime.cc:655]   | stack=0xff3a8000-0xff3aa000 stackSize=8192KB
runtime.cc:655]   | held mutexes= "abort lock" "mutator lock"(shared held)

我的代码(结构)

// MainActivity
public static ArrayList<Doc> DocList;

public class Doc {
    private String name;
    private String date;
    private ArrayList<ImageModel> imageList;
}

public class ImageModel {
    private Bitmap mBitmap;
}

我的函数

private void saveData(Object arrayTarget) {
    SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    Gson gson = new Gson();
    String json = gson.toJson(arrayTarget);
    editor.putString("task list", json);
    editor.apply();
}

private void loadData() {
    SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
    Gson gson = new Gson();
    String json = sharedPreferences.getString("task list", null);
    Type type = new TypeToken<ArrayList<Doc>>() {}.getType();
    DocList = gson.fromJson(json, type);
    if (DocList == null) {
        DocList = new ArrayList<Doc>();
    }
}

好的,我又解决了我的问题,只需将数组列表中的图片一张一张地保存在某个指定文件夹中,您就知道它是该数组的那个文件夹。
在代码中它将保存在内部存储中 (data/data/com.xxx.xxx/files).

private void saveImageBitmap(Bitmap path){
    File path = new File(getFilesDir(), path.toString());

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(path);
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}