Issue
I am knew to Android Studio. I want to create folder in external storage through android app. I tried using getExternalStorageDir()
but it is not implemented for API level above 23. Please guide me to create folder and file in android app. Ignore any grammatical mistake if present. And I also want this process to be done in background without going anywhere from mainactivity.
Solution
The user can choose to make a new folder while using the SAF picker, but you can't programmatically create a folder in external storage without any user interaction.
For instance, add a Button to your layout and give it an OnClickListener. Then have it run this code when clicked:
int OPEN_REQUEST_CODE = 41;
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TITLE, "Notes");
startActivityForResult(intent, OPEN_REQUEST_CODE);
This will prompt the user to create a document titled "Notes", but the user can choose where the document is stored and can even rename it if they so choose. Within the picker, there's an option named "New folder" that the user can select.
Part of the idea by Scoped Storage is that they don't want us devs to just be polluting the external storage with all sorts of files and folders. So this puts the user more in control.
You can make a subfolder once the user chooses a folder, but this still requires prompting the user to grant one-time access to a chosen folder:
https://stackoverflow.com/a/36547137/7434090
Also, this tutorial was very helpful to me while learning SAF:
https://en.proft.me/2018/05/24/using-android-storage-access-framework/
Answered By - Gavin Wright
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.