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

141,372

Solution 1

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"));
}

Solution 2

This should help you:

Uri selectedUri = Uri.parse(Environment.getExternalStorageDirectory() + "/myFolder/");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "resource/folder");

if (intent.resolveActivityInfo(getPackageManager(), 0) != null)
{
    startActivity(intent);
}
else
{
    // if you reach this place, it means there is no any file 
    // explorer app installed on your device
}

Please, be sure that you have any file explorer app installed on your device.

EDIT: added a shantanu's recommendation from the comment.

LIBRARIES: You can also have a look at the following libraries https://android-arsenal.com/tag/35 if the current solution doesn't help you.

Solution 3

Thread is old but I needed this kind of feature in my application and I found a way to do it so I decided to post it if it can help anyone in my situation.

As our device fleet is composed only by Samsung Galaxy Tab 2, I just had to find the file explorer's package name, give the right path and I succeed open my file explorer where I wanted to. I wish I could use Intent.CATEGORY_APP_FILES but it is only available in API 29.

  Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.sec.android.app.myfiles");
  Uri uri = Uri.parse(rootPath);
  if (intent != null) {
      intent.setData(uri);
      startActivity(intent);
  }

As I said, it was easy for me because our clients have the same device but it may help others to find a workaround for their own situation.

Solution 4

Intent chooser = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getDownloadCacheDirectory().getPath().toString());
chooser.addCategory(Intent.CATEGORY_OPENABLE);
chooser.setDataAndType(uri, "*/*");
// startActivity(chooser);
try {
startActivityForResult(chooser, SELECT_FILE);
}
catch (android.content.ActivityNotFoundException ex)
{
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}

In code above, if setDataAndType is "*/*" a builtin file browser is opened to pick any file, if I set "text/plain" Dropbox is opened. I have Dropbox, Google Drive installed. If I uninstall Dropbox only "*/*" works to open file browser. This is Android 4.4.2. I can download contents from Dropbox and for Google Drive, by getContentResolver().openInputStream(data.getData()).

Solution 5

Here is my answer

private fun openFolder() {
    val location = "/storage/emulated/0/Download/";
    val intent = Intent(Intent.ACTION_VIEW)
    val myDir: Uri = FileProvider.getUriForFile(context, context.applicationContext.packageName + ".provider", File(location))
    intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        intent.setDataAndType(myDir,  DocumentsContract.Document.MIME_TYPE_DIR)
    else  intent.setDataAndType(myDir,  "*/*")

    if (intent.resolveActivityInfo(context.packageManager, 0) != null)
    {
        context.startActivity(intent)
    }
    else
    {
        // if you reach this place, it means there is no any file
        // explorer app installed on your device
        CustomToast.toastIt(context,context.getString(R.string.there_is_no_file_explorer_app_present_text))
    }
}

Here why I used FileProvider - android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()

I tested on this device
Devices: Samsung SM-G950F (dreamltexx), Os API Level: 28

Share:
141,372

Related videos on Youtube

kaolick
Author by

kaolick

Professional mobile app developer

Updated on July 08, 2022

Comments

  • kaolick
    kaolick almost 2 years

    I thought this would be easy but as it turns out unfortunately it's not.

    What I have:

    I have a folder called "myFolder" on my external storage (not sd card because it's a Nexus 4, but that should not be the problem). The folder contains some *.csv files.

    What I want:

    I want to write a method which does the following: Show a variety of apps (file browsers) from which I can pick one (see picture). After I click on it, the selected file browser should start and show me the content of "myFolder". No more no less.

    enter image description here

    My question:

    How exactly do I do that? I think I came quite close with the following code, but no matter what I do - and I'm certain that there must be something I didn't get right yet - it always opens only the main folder from the external storage.

    public void openFolder()
    {
    File file = new File(Environment.getExternalStorageDirectory(),
        "myFolder");
    
    Log.d("path", file.toString());
    
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setDataAndType(Uri.fromFile(file), "*/*");
    startActivity(intent);
    }
    
    • Ameen Maheen
      Ameen Maheen almost 8 years
      Try this one covers almost every file extension
    • NoWar
      NoWar about 6 years
      Hi man! Please take a look here stackoverflow.com/questions/50072638/… and help to resolve similiar issue. Thank you!
    • Bip901
      Bip901 about 2 years
      Android hasn't standardized this feature. Only some file managers support it - see this highly relevant GitHub issue.
  • kaolick
    kaolick about 11 years
    No, unfortunately this does NOT work. Same result as with my code above.
  • Guian
    Guian about 11 years
    what's in the 'myUri' object before to send it to the intent ?
  • kaolick
    kaolick about 11 years
    What do you mean by "what is in the URI"? Do you mean sth like uri.toString()?
  • Ali Behzadian Nejad
    Ali Behzadian Nejad over 10 years
    Some apps like OI file manager work with this Intent but Android's default file manager does not work this method.
  • Suresh Sharma
    Suresh Sharma over 10 years
    This is just working for DropBox for me. Can you please give modified code for opening Folders in SDcard.
  • kaolick
    kaolick over 10 years
    @SureshSharma Which file browser do you want to use for opening folders on your SD-card?
  • shantanu
    shantanu over 9 years
    User this code to avoid crash when no application installed. Intent chooser = Intent.createChooser(intent, title); // Verify the intent will resolve to at least one activity if (intent.resolveActivity(getPackageManager()) != null) { startActivity(chooser); }
  • Ayaz Alifov
    Ayaz Alifov over 8 years
    @shantanu, thank you for your recommendation, I added a similar method to check existence of an intent.
  • Admin
    Admin over 8 years
    Have you tried on a device without sd card? In my case on several devices it this approach can not handle intent, as like there are no file browsers, but there are few of them.
  • Thilina H
    Thilina H about 8 years
    This has to be marked as the answer... The trick is to install a file explorer.. Es file explorer worked for me but not OI file manager...
  • ban-geoengineering
    ban-geoengineering over 7 years
    I had to replace Uri.fromFile(...) with Uri.parse(...) - as shown in OP's question and the accepted answer.
  • abegosum
    abegosum over 7 years
    I'm not sure if this is an API change; but, Environment.getExternalStorageDirectory() returns a File, while android.net.Uri.parse expects a string. I resolved this by using Environment.getExternalStorageDirectory().getPath() as the argument. (This assumes you want the base directory of external storage as your starting point.)
  • PriyankaChauhan
    PriyankaChauhan almost 7 years
    This is working fine but in saumsung phone it not redirecting to specific folder
  • Jiawei Yang
    Jiawei Yang over 6 years
    Hey dude, this only works on devices with ES file explorer installed because mime 'resource/folder' is not a standard mime type.
  • Shashank Kadne
    Shashank Kadne over 6 years
    This indeed only appears to work with ES File Explorer. Let me know if there is a solution that works with other explorer Apps.
  • NoWar
    NoWar about 6 years
    Hi! Please take a look at the similar question stackoverflow.com/questions/50072638/…
  • Sam Chen
    Sam Chen almost 4 years
    getExternalStorageDirectory() has been deprecated. Any new solution?
  • Sam Chen
    Sam Chen almost 4 years
    How to specify the uri? I tried val filePath = Uri.parse(Environment.DIRECTORY_DOCUMENTS + "/myFolder/") doesn't work.
  • j__m
    j__m almost 4 years
    @SamChen as obtained from the Storage Access Framework are the keywords here. This does not work with file: URIs.
  • Akash khan
    Akash khan almost 4 years
    In my android studio, it shows error in startActivity Expected 3 arguments but found 1
  • Slion
    Slion about 3 years
    That works with other file explorer apps though, not just ES, so I guess that's more or less been standardized by third-party app developers.
  • Slion
    Slion about 3 years
    Do I need to setup the file provider in manifest then?
  • Slion
    Slion about 3 years
    No idea how you got this working, getUriForFiles throws out-of-bounds exception when passing a folder as explained there: stackoverflow.com/questions/54385933/…
  • Slion
    Slion about 3 years
    You may have thought it worked because you specified the download folder. Thing is it won't work when specifying other folders.
  • andras
    andras over 2 years
    This doesn't even open the application selector from a notification. It just doesn't do anything.
  • Noah
    Noah over 2 years
    getExternalStorageDirectory() has been deprecated. Any new solution? ... Use getExternalFilesDir(null)