Answer the question
In order to leave comments, you need to log in
How to serialize object to file on sd card in android?
I found a code that allows you to write an object to a file, but this file will not be on the sd card.
public static Object readFromFile(String filename, Context content) {
Object object = null;
try {
FileInputStream fis = content.openFileInput("filesLifeOrganizationRe" + filename + ".txt");
ObjectInputStream is = new ObjectInputStream(fis);
object = is.readObject();
is.close();
} catch (IOException w) {
w.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return object;
}
public static void writeToFile(String filename, Object object, Context context) {
try {
FileOutputStream fos = context.openFileOutput("filesLifeOrganizationRe" + filename + ".txt", Context.MODE_WORLD_READABLE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(object);
os.close();
} catch (IOException w) {
w.printStackTrace();
}
}
Answer the question
In order to leave comments, you need to log in
The SD card has the same compatible file system, but permission is needed WRITE_EXTERNAL_STORAGE
, and on some devices it can be a problem to get the path to this card, but usually everything is standard and the path looks like
Environment.getExternalStorageDirectory() + "/file.html
, well, and a special check for the presence of an SD card is also desirable (not required), likeEnvironment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question