I am trying to open a directory with the help of an intent to show the user what is the content inside that folder but I am unable to do so, but I don't know why the folder won't open and I get this "Can't use this folder" on the file manager.
open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/" + "XYZ";
Uri uri = Uri.parse(path);
Toast.makeText(MainActivity.this, ""+path, Toast.LENGTH_SHORT).show();
openDirectory(uri);
}
});
The method
public void openDirectory(Uri uriToLoad){
// Choose a directory using the system's file picker.
int result = 1;
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
// Optionally, specify a URI for the directory that should be opened in
// the system file picker when it loads.
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uriToLoad);
startActivityForResult(intent, result);
}
I want to open the folder XYZ after clicking on the button
I am trying to open a directory with the help of an intent to show the user what is the content inside that folder
There is no standard Intent
action for that, sorry. Your code is trying to let the user select a document tree.
It is also doing that incorrectly, as EXTRA_INITIAL_URI
does not take a file://
Uri
as a value. That needs to be some Uri
that you obtained previously from the Storage Access Framework, such as via some past ACTION_OPEN_DOCUMENT_TREE
request.
I don't know why the folder won't open and I get this "Can't use this folder" on the file manager
From Android's standpoint, your EXTRA_INITIAL_URI
value is little better than a random string.
But the user won't know where actually XYZ folder is
Then perhaps you should have let the user choose the location in the first place, rather than forcing a particular location. For example, you could use ACTION_CREATE_DOCUMENT
to let the user decide where to place the document on the user's device (or the user's cloud storage, the user's network file server, etc.).
As far as I know using Intent you can browse, open and create files, that shared for all apps by the system. In this case for get file path to open, you can use next code like this (pardon for my Kotlin):
private var launcherForResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
result.data.also { uri -> filePath = uri.toString() }
}
}
private fun getFilePath() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
type = "*/*"
addCategory(Intent.CATEGORY_OPENABLE)
}
launcherForResult.launch(intent)
}
As for the directory, it usually opens the last one that was opened before.
If you don't want other applications to see your files, store them in your application's private directory
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.