Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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); } 
like image 687
kaolick Avatar asked Jun 18 '13 10:06

kaolick


People also ask

How do I open a folder on Android?

Just open it up to browse any area of your local storage or a connected Drive account; you can either use the file type icons at the top of the screen or, if you want to look folder by folder, tap the three-dot menu icon in the upper-right corner and select "Show internal storage" — then tap the three-line menu icon in ...

How do I open file explorer in Android programmatically?

Intent intent = new Intent(Intent. ACTION_GET_CONTENT); intent. setType("*/*"); Intent i = Intent. createChooser(intent, "View Default File Manager"); startActivityForResult(i, CHOOSE_FILE_REQUESTCODE);

How do I get a list of images in a specific folder on Android?

You can use below code to get all images from specific folder. 1) First you need to define File object to get the storage and appened the name of the folder you want to read. File folder = new File(Environment. getExternalStorageDirectory().

How do I open a folder in Android Studio?

Right-click on a file or directory to create a new file or directory, save the selected file or directory to your machine, upload, delete, or synchronize. Double-click a file to open it in Android Studio. Android Studio saves files you open this way in a temporary directory outside of your project.


Video Answer


2 Answers

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.

like image 60
Ayaz Alifov Avatar answered Oct 19 '22 02:10

Ayaz Alifov


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")); } 
like image 31
kaolick Avatar answered Oct 19 '22 04:10

kaolick