Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View folder contents in Android using default explorer

I want to programatically launch a default file explorer to show me the contents of a folder.

I'm using this code, but it crashes:

    Uri startDir = Uri.fromFile(new File("/sdcard/DCIM/Camera")); 
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_VIEW); 
    intent.setData(startDir);
    startActivity(intent);

LogCat shows "No Activity found to handle the Intent"...

What's my best option to do this? I want the user to see the contents of the folder and be able to click the files (e.g. click a video and launch it with default player, click a PDF and open it etc).

like image 866
Bogdan Alexandru Avatar asked Jul 20 '26 00:07

Bogdan Alexandru


1 Answers

Unfortunately there seem to be no standard way to do this, I was searching for the exact same thing before and couldn't find out any solution. There are 2 alternative methods that might work for you:

1) Use a general intent and let the user pick his/her file manager This is safe and easy but a little far from what we really want

Uri startDir = Uri.fromFile(new File(Environment.getExternalStorageDirectory()
        .getAbsolutePath() + "/DCIM/Camera"));
    Intent intent = new Intent();
    intent.setData(startDir);
    intent.setType("*/*");
    intent.setAction(Intent.ACTION_VIEW);
    startActivity(intent);

Don't use intent.setType("file/*");, it's not a standard MIME type.

2) Use Specific Intents that the famous file managers provide The well known file managers have their own custom intent filters that accept a directory path which allows simple browsing. Some of them are here: OI file manager, ES Explorer

Maybe you could check if the user have these specific file managers installed and then use this solution, else fallback to general intent.

For now these are the only two options you have. I will update this post if I find out any better solution.

like image 102
Nima G Avatar answered Jul 22 '26 14:07

Nima G