0

我不知道如何让这段代码工作。我的第一个问题是我不确定我的情况是否file.exist()正常。这段代码应该在 SD 卡上查看是否有文件,然后保存我的序列化对象并添加新数据,然后再次保存。如果文件不在 SD 卡上,那么它应该创建它。我从日志中得到的唯一信息是:

09-09 18:48:45.241  15415-15415/com.dami.CourierServiceee V/LOGSD: read from SD
09-09 18:48:45.361  15415-15415/com.dami.CourierServiceee V/LOGSD: fread from SD works?
09-09 18:48:45.361  15415-15415/com.dami.CourierServiceee V/LOGSD: repeated wrote -------

这是我的代码

   private void SaveDataToSDCard(List<PictureObject> listsave) {

    String filename = "pictures.data";
    String root = Environment.getExternalStorageDirectory().toString();
    File dir = new File(root + "/courier/saved/");
    File file = new File(dir,filename);

    if (file.exists()){
        // restore data from SD card and add new data to list and then save them to SD card
        try {
            if (!dir.exists()) {
                Log.v("FileIOService", "No Such Directory Exists");
            }

            ListOflists = new ArrayList<List<PictureObject>>();
            Log.v("LOGSD", "read from SD");
            ListOflists = RestoreDataFromSDCard(file,dir);
            Log.v("LOGSD", "read from SD works?");

            ListOflists.add(listsave);
            SerializePictureObject serialize1 = new SerializePictureObject(ListOflists);

            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            Log.v("LOGSD", "repeated wrote -------");
            oos.writeObject(serialize1);
            oos.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }else{
        // write data to SD card
        FileOutputStream fileOutputStream = null;
        ObjectOutputStream objectOutputStream = null;

        try {
            if (!dir.exists()) {
                dir.mkdirs();
            }

            fileOutputStream = new FileOutputStream(file);
            objectOutputStream = new ObjectOutputStream(fileOutputStream);

            Log.v("LOGSD", "  saving to SD ------------");
            ListOflists = new ArrayList<List<PictureObject>>();
            ListOflists.add(listsave);
            SerializePictureObject serialize = new SerializePictureObject(ListOflists);

            objectOutputStream.writeObject(serialize);
            objectOutputStream.close();
            Log.v("LOGSD", " saving o SD");

        } catch (IOException ioException) {
            ioException.getMessage();
        } catch (Exception e) {
            e.getMessage();
        }
    }
}

public static List<List<PictureObject>> RestoreDataFromSDCard(File file,File dir){

    FileInputStream fistream = null;
    ObjectInputStream oistream = null;

    List<List<PictureObject>> pomlist = new ArrayList<List<PictureObject>>();
    SerializePictureObject pom;

    try {
        if (!dir.exists()) {
            Log.v("FileIOService", "No Such Directory Exists restoredatafromsdcard");
        }
        fistream = new FileInputStream(file);
        oistream = new ObjectInputStream(fistream);
         pom = (SerializePictureObject) oistream.readObject();
        Log.v("LOGSD", " behem nacitani z SD karty");
         pomlist = pom.get_serializeList();
        oistream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return pomlist;
}

我会很高兴得到任何帮助:)

java.io.WriteAbortedException: Read an exception; java.io.NotSerializableException: com.dami.CourierServiceee.FinishHandOver$PictureObject
Caused by: java.io.NotSerializableException: com.dami.CourierServiceee.FinishHandOver$PictureObject
java.io.NotSerializableException: com.dami.CourierServiceee.FinishHandOver$PictureObject

这是 serializepictureobject 保存 PictureObjecs 和 PictureObjecs 保存位图、字符串、字符串、字符串的列表

    public class SerializePictureObject implements Serializable {

    private static final long serialVersionUID = 123456789;
    List<List<PictureObject>> _serializeList = null;

    public SerializePictureObject(List<List<PictureObject>> _serializeList) {
        this._serializeList = _serializeList;
    }

    public List<List<PictureObject>> get_serializeList() {
        return _serializeList;
    }

    public void set_serializeList(List<List<PictureObject>> _serializeList) {
        this._serializeList = _serializeList;
    }
}

编辑:也许问题是这样的:

09-10 11:19:13.671      620-620/com.dami.CourierServiceee W/System.err: java.io.WriteAbortedException: Read an exception; java.io.NotSerializableException: android.graphics.Bitmap
09-10 11:19:13.701      620-620/com.dami.CourierServiceee W/System.err: Caused by: java.io.NotSerializableException: android.graphics.Bitmap
09-10 11:19:13.821      620-620/com.dami.CourierServiceee W/System.err: java.io.NotSerializableException: android.graphics.Bitmap
4

1 回答 1

0

也许添加这个

fos.flush();
fos.close();
于 2013-09-10T10:58:45.753 回答