Android: How to Open a Specific Folder Via Intent and Show Its Content in a File Browser

Android: How to open a specific folder via Intent and show its content in a file browser?

I finally got it working. This way only a few apps are shown by the chooser (Google Drive, Dropbox, Root Explorer, and Solid Explorer). It's working fine with the two explorers but not with Google Drive and Dropbox (I guess because they cannot access the external storage). The other MIME type like "*/*" is also possible.

public void openFolder(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ File.separator + "myFolder" + File.separator);
intent.setDataAndType(uri, "text/csv");
startActivity(Intent.createChooser(intent, "Open folder"));
}

How to open specific folder in the storage using intent in android

This code may help you:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, YOUR_RESULT_CODE);

If you want to open a specific folder:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

intent.setDataAndType(Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ File.separator + "myFolder" + File.separator), "file/*");
startActivityForResult(intent, YOUR_RESULT_CODE);

How to simply open directory/folder?

The solution (not complete) I have found, was that I was missing file:// prefix. Here is my solution (however, it shows all kinds of applications on first view):

public void openFolder(String location)
{
// location = "/sdcard/my_folder";
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri mydir = Uri.parse("file://"+location);
intent.setDataAndType(mydir,"application/*"); // or use */*
startActivity(intent);
}

p.s. Strange and surprising, but there doesnt exist the standard definition of "File Browser" in stock Android systems (unless you install 3rd party "File Explorer")..
That's why "resource/folder" mime-type doesnt work by default..

However, let's say a simple truth. File-Browser is a SIMPLE and ESSENTIAL part of any OS system. And it's quite disrespectful from Android, saying that it's not their job, and throwing the responsiblity to 3rd party apps.

How to gain access to a specific folder and write a file into it using Scoped Storage

DocumentFile is Google's somewhat clunky solution for navigating through document trees. The recipe for what you want should be:

  • Take the Uri that you got for your tree and wrap it in a DocumentFile using DocumentFile.fromTreeUri()
  • Call createFile() on that DocumentFile, supplying a filename or other display name, which will give you a DocumentFile representing that document
  • Call getUri() on the document's DocumentFile to get a Uri representing the document
  • Use openOutputStream() on a ContentResolver to write your desired content to that Uri

Note that if you need long-term access to this tree, be sure to call takePersistableUriPermission() on a ContentResolver, passing in the Uri for the document tree. That should give you access to the tree and all documents inside of it.



Related Topics



Leave a reply



Submit