0

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.

Abra
  • 19,142
  • 7
  • 29
  • 41
  • `And I also want this process to be done in background without going anywhere from mainactivity.` Using SAF you should at least invoke the user once to let him choose a folder. – blackapps Sep 04 '20 at 06:21

2 Answers2

0

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/

Gavin Wright
  • 3,124
  • 3
  • 14
  • 35
  • 1
    `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.` Can be. But with using SAF you can pollute even more as complete external storage becomes writable. Not only primary partition but complete micro sd card. – blackapps Sep 04 '20 at 06:18
  • The result of this just creates an empty file, not a folder. You should use Intent.ACTION_OPEN_DOCUMENT_TREE as suggested by the link the answer points to – clementiano Aug 30 '21 at 13:03
0

Put this in public void

Intent intent = new  
Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); 
startActivityForResult(intent, NEW_FOLDER_REQUEST_CODE);

This on Activity Result

if (_resultCode == Activity.RESULT_OK) { if (_requestCode == 
NEW_FOLDER_REQUEST_CODE) 
{ if (_data != null) { Uri currentUri = _data.getData();
DocumentFile pickedDir =
DocumentFile.fromTreeUri(this, currentUri);
DocumentFile newDir =
pickedDir.createDirectory("MyFolder"); } } }

This is another public void on oncreate

}
private static final int NEW_FOLDER_REQUEST_CODE = 43;
}
J. M.
  • 115
  • 6