简体   繁体   中英

Where should I choose to save text file in android?

I hope to export my data as a text file and save it to disk in Android, so I need to choose which folder I will save the file to.

I hope that a normal user can find the folder easily and the app does not need special permission to create the folder.

I have read some document, it seems that there are 3 ways: Context.getFilesDir().getAbsolutePath() , Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null) .

You know some android users don't install SD card, so it seems that Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null) are be excluded.

Am I only to choose Context.getFilesDir().getAbsolutePath() ? or is there a better way? Thanks!

BTW, From the document Android - Where to save text files to?

Save it in internal phone storage, here no users and applications can access these files(unless if phone is rooted). But these files will be deleted one's the user selectes clear data from Settings -> Apps -> .

It seems that normal users can't access the saved text files if I use Context.getFilesDir().getAbsolutePath(), is it right?

Use this if you want a path that the user can modify and can have access

getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();

More documentation here .

EDIT:

This is how use in case error in some devices:

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
String fname = "TEXT.txt";
File file = new File(path, fname);

if (!path.exists()) {
//noinspection ResultOfMethodCallIgnored
path.mkdir();
}

// YOUR CODE FOR COPY OR CREATE THE FILE TXT in PATH WITH THE VARIABLE file ABOVE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM