112

I would like to start an intentchooser for apps which can return any kind of file

Currently I use (which I copied from the Android email source code for file attachment)

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
Intent i = Intent.createChooser(intent, "File");
startActivityForResult(i, CHOOSE_FILE_REQUESTCODE);

But it only shows "Gallery" and "Music player" on my Galaxy S2. There is a file explorer on this device and I would like it to appear in the list. I would also like the camera app to show in the list, so that the user can shoot a picture and send it through my app. If I install Astro file manager it will respond to that intent, too. My customers are Galaxy SII owners only and I don't want to force them to install Astro file manager given that they already have a basic but sufficient file manager.

Any idea of how I could achieve this ? I am pretty sure that I already saw the default file manager appear in such a menu to pick a file, but I can't remember in which app.

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
ErGo_404
  • 1,801
  • 4
  • 18
  • 22

8 Answers8

95

Not for camera but for other files..

In my device I have ES File Explorer installed and This simply thing works in my case..

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
user370305
  • 108,599
  • 23
  • 164
  • 151
56

Samsung file explorer needs not only custom action (com.sec.android.app.myfiles.PICK_DATA), but also category part (Intent.CATEGORY_DEFAULT) and mime-type should be passed as extra.

Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);

You can also use this action for opening multiple files: com.sec.android.app.myfiles.PICK_DATA_MULTIPLE Anyway here is my solution which works on Samsung and other devices:

public void openFile(String mimeType) {

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType(mimeType);
        intent.addCategory(Intent.CATEGORY_OPENABLE);

        // special intent for Samsung file manager
        Intent sIntent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
         // if you want any file type, you can skip next line 
        sIntent.putExtra("CONTENT_TYPE", mimeType); 
        sIntent.addCategory(Intent.CATEGORY_DEFAULT);

        Intent chooserIntent;
        if (getPackageManager().resolveActivity(sIntent, 0) != null){
            // it is device with Samsung file manager
            chooserIntent = Intent.createChooser(sIntent, "Open file");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { intent});
        } else {
            chooserIntent = Intent.createChooser(intent, "Open file");
        }

        try {
            startActivityForResult(chooserIntent, CHOOSE_FILE_REQUESTCODE);
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(getApplicationContext(), "No suitable File Manager was found.", Toast.LENGTH_SHORT).show();
        }
    }

This solution works well for me, and maybe will be useful for someone else.

flipm0de
  • 183
  • 1
  • 10
Chupik
  • 1,040
  • 8
  • 13
  • It almost works for me, except the fact that sIntent, when launched, accepts no file of any type. I can browse through folders, but that's it. – Radu Nov 08 '13 at 11:24
  • The problem is that the intent needs to have mimeType: "file/*", while the Samsung sIntent needs " * / *" - without the spaces between * / * – Radu Nov 08 '13 at 11:30
  • where does the string `com.sec.android.app.myfiles.PICK_DATA` comes from ? which part of it is dynamic ? – Francisco Corrales Morales Nov 26 '14 at 17:09
  • Thanks, it's working for me on 4 of my different devices, so it's pretty much universal! – bendaf Apr 05 '16 at 11:14
  • 1
    What about the other 11k of Android devices. – Oliver Dixon May 21 '16 at 12:15
  • How to specify the starting location/uri for the file chooser? – Darush May 17 '17 at 10:53
  • 1
    How set URI for this intent can you please help me ? – PriyankaChauhan Jul 27 '17 at 06:03
  • How to pass the folderpath for this intent, so that FilePicker will open in specific folder directly? – Santyy Mar 27 '18 at 07:50
  • If you want to specify URI you should probably use ACTION_PICK instead of ACTION_GET_CONTENT https://stackoverflow.com/questions/17765265/difference-between-intent-action-get-content-and-intent-action-pick – Chupik Mar 27 '18 at 08:08
  • 1
    In case of Samsung Intent ("com.sec.android.app.myfiles.PICK_DATA") you can try to put String extra "FOLDERPATH" or "START_FOLDER" – Chupik Mar 27 '18 at 08:26
  • @Chupik intent.PutExtra("FOLDERPATH", path); works perfectly. Thank You – Santyy Mar 27 '18 at 09:00
45

this work for me on galaxy note its show contacts, file managers installed on device, gallery, music player

private void openFile(Int  CODE) {
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.setType("*/*");
    startActivityForResult(intent, CODE);
}

here get path in onActivityResult of activity.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     String Fpath = data.getDataString();
    // do somthing...
    super.onActivityResult(requestCode, resultCode, data);

}
alireza
  • 451
  • 4
  • 2
15

This gives me the best result:

    Intent intent;
    if (android.os.Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
        intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
        intent.putExtra("CONTENT_TYPE", "*/*");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
    } else {

        String[] mimeTypes =
                {"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx
                        "application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation", // .ppt & .pptx
                        "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xls & .xlsx
                        "text/plain",
                        "application/pdf",
                        "application/zip", "application/vnd.android.package-archive"};

        intent = new Intent(Intent.ACTION_GET_CONTENT); // or ACTION_OPEN_DOCUMENT
        intent.setType("*/*");
        intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
    }
Islam Khaled
  • 221
  • 3
  • 10
  • Can you share your onActivityResultCode @Islam Khaled – Kishan Donga Apr 27 '20 at 12:56
  • 1
    See also org.openintents.action.PICK_FILE for a third option. http://www.openintents.org/action/org-openintents-action-pick-file/ Your code also assumes that everyone on a Samsung phone uses and (still) has the Samsung file manager. Using intent.resolveActivity with Samsung as a fallback would be more robust. – Marcus Wolschon May 12 '20 at 07:04
  • Thank you. This was the only way I managed to allow users choose files from multiple types (in my case I only need pdf and txt). – ice_chrysler Sep 05 '22 at 13:45
1

Turns out the Samsung file explorer uses a custom action. This is why I could see the Samsung file explorer when looking for a file from the samsung apps, but not from mine.

The action is "com.sec.android.app.myfiles.PICK_DATA"

I created a custom Activity Picker which displays activities filtering both intents.

ErGo_404
  • 1,801
  • 4
  • 18
  • 22
  • Hi, can you explain this a bit more? For samsung galaxy phones I use Intent selectFile = new Intent( "com.sec.android.app.myfiles.PICK_DATA"); but it causes an error – steliosf Apr 30 '12 at 00:41
  • I'm not sure right now, but I can check it later. I think it's the Activity Not Found exception. Do you have any working solution? I almost tried everything and didn't manage to fix it – steliosf May 29 '12 at 20:21
  • This intent has worked for me on the Galaxy SII. Tried it on a GT-B5510 (Galaxy Y Pro) and it didn't work. I think it is just not reliable if you want to target all Samsung devices. I ended up integrating an open source file manager into my application, but it is a heavy solution for such a basic need. – ErGo_404 Jun 04 '12 at 07:38
  • How set URI for this intent can you please help me ? – PriyankaChauhan Jul 27 '17 at 06:04
0

If you want to know this, it exists an open source library called aFileDialog that it is an small and easy to use which provides a file picker.

The difference with another file chooser's libraries for Android is that aFileDialog gives you the option to open the file chooser as a Dialog and as an Activity.

It also lets you to select folders, create files, filter files using regular expressions and show confirmation dialogs.

danigonlinea
  • 1,113
  • 1
  • 14
  • 20
0

The other answers are not incorrect. However, now there are more options for opening files. For example, if you want the app to have long term, permanent acess to a file, you can use ACTION_OPEN_DOCUMENT instead. Refer to the official documentation: Open files using storage access framework. Also refer to this answer.

Miloš Černilovský
  • 3,846
  • 1
  • 28
  • 30
0

ES File Explorer no longer exists on Google Play, as an alternative you can use FS File Explorer, this application allows the selection of any type of file. In time I made a library that facilitates communication with FS File Explorer: https://github.com/YounesHass/fs-selection

Younes
  • 41
  • 1
  • 6