You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you want to save files on external storage that are private to your app and not accessible by the MediaStore content provider, you can acquire a directory that's used by only your app by calling getExternalFilesDir() and passing it a name indicating the type of directory you'd like. Each directory created this way is added to a parent directory that encapsulates all your app's external storage files, which the system deletes when the user uninstalls your app (read more).
Using the following method to write file in the external directory, you don`t need to request for permissions.
Files on external storage are not always accessible, because users can mount the external storage to a computer for use as a storage device. So if you need to store files that are critical to your app's functionality, you should instead store them on internal storage.
If none of the pre-defined sub-directory names suit your files, you can instead call getExternalFilesDir() and pass null. This returns the root directory for your app's private directory on the external storage.
Remember that getExternalFilesDir() creates a directory that is deleted when the user uninstalls your app. If the files you're saving should remain available after the user uninstalls your app—such as when your app captures photos and the user should keep those photos—you should instead save the files to a public directory.
publicclassMainActivityextendsAppCompatActivity {
privatestaticfinalStringLOG_TAG = MainActivity.class.getSimpleName();
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendarcalendar = Calendar.getInstance();
StringtimeStamp/* use as file name */ = String.valueOf(calendar.getTimeInMillis());
Stringtext_to_save = "Created on " + timeStamp;
//Save the text_to_save in a file inside the private Directory of the applicationsaveFileInPrivateDirectory(this, timeStamp + ".txt", text_to_save);
}
/** * @param context the current context. * @param folderName represents the folder name to use. * @return FileSystem object representing the platform's local file system. */publicFilegetPrivateStorageDirectory(Contextcontext, StringfolderName) {
// Get the directory for the app's private downloads directory.Filefile = newFile(context.getExternalFilesDir(
Environment.DIRECTORY_DOWNLOADS), folderName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
returnfile;
}
/** * Creates a file, writes in a string and saves it in the private Storage * directory of the application. Writing the private directory of the application * does not require specific permissions. * Location may vary but here is what I have while testing this: * /storage/emulated/0/Android/data/mg.studio.writefileexternalstorage/files/Download/directory * * @param context the current context * @param sFileName the file name to create to save the string * @param sBody string to save in the text file */publicvoidsaveFileInPrivateDirectory(Contextcontext, StringsFileName, StringsBody) {
StringfolderName = "directory";
try {
Fileroot = getPrivateStorageDirectory(context, folderName);
Filefile = newFile(root, sFileName);
FileWriterwriter = newFileWriter(file);
writer.append(sBody);
writer.flush();
writer.close();
Log.d(LOG_TAG, "Saved: " + root.toString());
} catch (IOExceptione) {
Log.e(LOG_TAG, "saveFileInPrivateDirectory(): " + e);
}
}
}