Kaloyan Roussev
Kaloyan Roussev

Reputation: 14711

How do I save a file to a subdirectory in InternalStorage

This is how I save files to InternalStorage

public static boolean saveInputStreamToInternalStorageFile(Context context, String filename, byte[] dataToWrite, Context ctx) {
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(context.getFilesDir() + File.separator + filename);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(dataToWrite);
        oos.close();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

How do I change that code, so that files are now saved into a subdirectory of that folder, called "media/"?

Upvotes: 1

Views: 55

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006604

Replace:

fos = new FileOutputStream(context.getFilesDir() + File.separator + filename);

with:

File media=new File(context.getFilesDir(), "media");

media.mkdirs();

fos = new FileOutputStream(new File(media, filename));

Upvotes: 4

Related Questions