Last Updated: March 23, 2016
·
1.359K
· brunoguic

Write to Internal, External, SD whatever file in Android

I've been looking for a directly post about it and I couln't found.

Android has Internal / External possible to write your data.

public void writeData(){
    File folder = getFolder();
    File file = new File(folder,"file.txt");
    FileOutputStream fos = new FileOutputStream(file);
    ObjectOutputStream os = new ObjectOutputStream(fos);
    os.writeChars("HelloAndroid");
}

public File getFolder(){
    //All this are options:
    return ctx.getDir(Context.MODE_PRIVATE);  //Internal
    return ctx.getExternalFilesDir(); // External Private
    return ctx.getExternalCacheDir();//External Private Cache
    return Environment.getExternalStoragePublicDirectory(); //External Shared
    return Environment.getExternalStorageDirectory(); // External Private Prior API7
}

The first question is What to use?

Internal - Only app can access, if unstall the app, data are deleted too.

External Private - Only your app can access, BUT the user can access too.

External Private Cache - Same External Private, but if unstall the app, data are deleted too.

External Shared - Your app and others apps, (and user) can access the data.

The second question is, How to put in SD card?
You cant put directly in SD Card, this is control by users preference and the phone's manufactory.

I hope to help someone =)

1 Response
Add your response

Thank you for this! Extremely simple reference that has helped quite a bit.

over 1 year ago ·