3

How to open a dialog that list all applications that can open a given folder?

I tried following code

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
startActivity(Intent.createChooser(intent, "Open Folder"));

it says "No applications can perform this action."

My device has default File Explorer and AndroidZip applications (these apps can open a folder).

dira
  • 30,304
  • 14
  • 54
  • 69

6 Answers6

5

it says "No applications can perform this action."

That is not surprising. You are not asking to open a "folder", as there is no such thing as a "folder" in the Intent system. You are trying to find out what applications can open a path with no file extension and no MIME type. There are no applications installed on your device that have an <intent-filter> on an activity that supports ACTION_GET_CONTENT on a path with no file extension and no MIME type.

You might try ACTION_VIEW. However, bear in mind that I would estimate that 90+% of Android devices will have nothing that deals with "folders" in this fashion, either.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you, CW! You're really a pro and do kindly to others more than a pro. – herbertD Apr 24 '12 at 08:28
  • One quick question, How can I a navigate to a folder? Say the folder contains all mp3. – Green goblin Jan 27 '14 at 18:51
  • @Aashish: If you read my answer, there is no such thing as a "folder" in the `Intent` system. – CommonsWare Jan 27 '14 at 18:52
  • Well, my scenario is when I click on a button, it should navigate to a given path. This is not possible? – Green goblin Jan 27 '14 at 18:54
  • @Aashish: If you read my answer, you are welcome to try `ACTION_VIEW`, but unless the user happens to have some sort of file manager app installed, it is unlikely to work. – CommonsWare Jan 27 '14 at 18:57
  • @CommonsWare How can we comes to know that the "Default File Manager" has the intent-filter "ACTION_VIEW" . Tried with this filter and mime intent.setDataAndType(uri, "resource/folder");// tried with application/pdf // and file/* but it nerver opens with default file manager. Works fine if device has ES file explorer(Most third party apps). Any solution. – Karthikeyan Ve May 27 '16 at 09:21
  • @KarthikeyanVe: Android does not have a "default file manager". There is no requirement for any device to have a file manager. The Nexus line of devices do not ship with one, for example. The closest thing is the Storage Access Framework standard UIs for `ACTION_OPEN_DOCUMENT` and `ACTION_CREATE_DOCUMENT`, on Android 4.4+. – CommonsWare May 27 '16 at 10:49
1

Try this here. Works for me.

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

See also here

Community
  • 1
  • 1
kaolick
  • 4,817
  • 5
  • 42
  • 53
1

Because files that are directories don't seem to have a standard mime type, I coded my file explorer to filter for intents without a mime type. Just set the data to your folder's Uri.

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/MyFolder");
intent.setData(uri);
startActivity(intent);

This intent will launch following app:

http://play.google.com/store/apps/details?id=com.organicsystemsllc.thumbdrive

I find this also works well to open either file or folder depending on extension. The file extension and mime type for folder's uri ends up being null in the code snippet below. This still works to match activities that filter for scheme “file” but no specific mime type, i.e. Thumb Drive. Note that this approach will not find activities that filter for all file mime types when uri is a directory.

//Get file extension and mime type
Uri selectedUri = Uri.fromFile(file.getAbsoluteFile());
String fileExtension =  MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString());
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);

//Start Activity to view the selected file
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, mimeType);
startActivity(Intent.createChooser(intent, "Open File..."));
Hugh Brady
  • 127
  • 4
1

It works:

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

Have a nice codding :)

Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
0

Unfortunately, like @CommonsWare said, there doesnt exist the standard definition of "File Explorer" in stock Android systems (unless you install 3rd party "File Explorer")..
That's why "resource/folder" mime-type doesnt work by default..

Maximum I've found, was this: https://stackoverflow.com/a/41614535/2377343

Community
  • 1
  • 1
T.Todua
  • 53,146
  • 19
  • 236
  • 237
0

I searched a lot and found the content type vnd.android.document/directory. Also, this includes other solutions for target API 24 and above. It showed the two apps to open it: OpenIntent File Manager and the built in File Manager.

This is research. And not a complete answer. If you find out what the arguments of the built-in file manager should be, let us know here.

File saveLocations i.e. points to the DCIM folder.

    // open a location with the file exporer
    // see https://stackoverflow.com/a/26651827/1320237
    // see https://stackoverflow.com/a/38858040
    // see https://stackoverflow.com/a/8727354
    Uri saveLocationUri =  FileProvider.getUriForFile(this, this.getPackageName() + ".provider", saveLocation);
    final Intent openSaveLocationIntent = new Intent(Intent.ACTION_VIEW);
    openSaveLocationIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    openSaveLocationIntent.setDataAndType(saveLocationUri, "vnd.android.document/directory");
    // see http://www.openintents.org/action/android-intent-action-view/file-directory
    openSaveLocationIntent.putExtra("org.openintents.extra.ABSOLUTE_PATH", saveLocation.toString());
    if (openSaveLocationIntent.resolveActivityInfo(getPackageManager(), 0) != null) {

                startActivity(openSaveLocationIntent);
    } else  {
        // if you reach this place, it means there is no any file
        // explorer app installed on your device

    }

It seems, there is a possiblity in Android 24+.

There is an intent filter to open directories:

    <activity
        android:name=".files.FilesActivity"
        android:documentLaunchMode="intoExisting"
        android:theme="@style/DocumentsTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="vnd.android.document/root" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="vnd.android.document/directory" />
        </intent-filter>
    </activity>

There is more code to it here: https://github.com/derp-caf/packages_apps_DocumentsUI/blob/ab8e8638c87cd5f7fe1005800520e739b3a48cd5/src/com/android/documentsui/ScopedAccessActivity.java#L114

I am not sure how and the arguments can be set correctly.

                // Sets args that will be retrieve on onCreate()
                final Bundle args = new Bundle();
                args.putString(EXTRA_FILE, file.getAbsolutePath());
                args.putString(EXTRA_VOLUME_LABEL, volumeLabel);
                args.putString(EXTRA_VOLUME_UUID, isPrimary ? null : storageVolume.getUuid());
                args.putString(EXTRA_APP_LABEL, appLabel);
                args.putBoolean(EXTRA_IS_ROOT, isRoot);
                args.putBoolean(EXTRA_IS_PRIMARY, isPrimary);

I tried to put in some extra information but it still opens the activity in the root folder.

    // from https://github.com/derp-caf/packages_apps_DocumentsUI/blob/ab8e8638c87cd5f7fe1005800520e739b3a48cd5/src/com/android/documentsui/ScopedAccessActivity.java#L114
    openSaveLocationIntent.putExtra("com.android.documentsui.FILE", saveLocation.toString());
    openSaveLocationIntent.putExtra("com.android.documentsui.IS_ROOT", false); // is the root folder?
    openSaveLocationIntent.putExtra("com.android.documentsui.IS_PRIMARY", true); // is the primary volume? SD-Card should be false
    openSaveLocationIntent.putExtra("com.android.documentsui.APP_LABEL", getTitle());
User
  • 14,131
  • 2
  • 40
  • 59