G
G
Gfd2016-08-21 20:26:00
Java
Gfd, 2016-08-21 20:26:00

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();
        }
    }

I would like to record to sd. How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rou1997, 2016-08-21
@Gfd

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), like
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question