Saving data to SD card and restoring them
I can't figure out how get this code to work. My first problem is I am not
sure if my condition file.exist() works how it should. This code should
look on an SD card to see if there is a file then save my serialized
object and add new data and then save it again. If the file isn't on the
SD card then it should create it. The only thing I get from the log is:
09-09 18:48:45.241 15415-15415/com.damidevelopment.CourierServiceee
V/LOGSD: read from SD
09-09 18:48:45.361 15415-15415/com.damidevelopment.CourierServiceee
V/LOGSD: fread from SD works?
09-09 18:48:45.361 15415-15415/com.damidevelopment.CourierServiceee
V/LOGSD: repeated wrote -------
Here is my code
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;
}
No comments:
Post a Comment