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 aDocumentFile
usingDocumentFile.fromTreeUri()
- Call
createFile()
on thatDocumentFile
, supplying a filename or other display name, which will give you aDocumentFile
representing that document - Call
getUri()
on the document'sDocumentFile
to get aUri
representing the document - Use
openOutputStream()
on aContentResolver
to write your desired content to thatUri
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
Recyclerview Indicates That Reaches the Last Item
How to Reduce an Image File Size Before Uploading to a Server
How to Retrieve Data from Sqlite Database in Android and Display It in Textview
How to Know Which Fragment the User Is Viewing
How to Know Location Area Code and Cell Id in Android Phone
How to Create Files to a Specific Folder in Android Application
Android: How to Scale a Bitmap to Fit the Screen Size Using Canvas/Draw Bitmap
Clear Cache in Android Application Programmatically
How to Stop Refreshing Recyclerview Data Scroll to Top Position Android Everytime
How to Edit Files Inside the Android Adb Shell
How to Get the Height of Recyclerview Item in "Onbindviewholder"
How to Synchronously Load Data from Firebase
How Capture/Log API Calls (Function Calls) Made by Android App
How to Change Listview Height Dynamically in Android
Prevent Other Applications Form Capturing/Recording Screen
How to Execute Adb Commands Through My Android App
Changing Background Color of the Layout on a Button Click in Android