dira
dira

Reputation: 30594

Open Folder - Intent chooser

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).

Upvotes: 4

Views: 18152

Answers (6)

User
User

Reputation: 14883

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());

Upvotes: 0

T.Todua
T.Todua

Reputation: 56557

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

Upvotes: 0

Ayaz Alifov
Ayaz Alifov

Reputation: 8608

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 :)

Upvotes: 1

Hugh Brady
Hugh Brady

Reputation: 127

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

Upvotes: 1

kaolick
kaolick

Reputation: 4857

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

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007534

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.

Upvotes: 6

Related Questions