Pick any kind of file via an Intent in Android
Asked Answered
S

8

114

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.

Septimal answered 20/1, 2012 at 17:38 Comment(6)
You will need very different code to shoot a picture then to choose a file. I don't actually think most file explorers can return a file, but I might be wrong.Huntress
can u specify what kind of files you need to be accessed primarily?Capitular
@Huntress : I dont expect the file explorer to return the file, I only need it's path.Septimal
@Capitular : I need to let the user pick any kind of file. It could be pdfs, .doc, .zip, ANY kind of file.Septimal
is it enough that you get the Uri of the specified file on the sd card???Capitular
github.com/criss721/Android-FileSelectorAlodee
A
96

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);
Amiamiable answered 20/1, 2012 at 18:21 Comment(9)
It does work with an external file explorer (such as ES File Explorer or Astro File Manager, but not with the Samsung file explorer. It seems odd that they did not implement the correct intent filters to respond to that action.Septimal
@Lukap - Use open source android file manager and integrate it with your application then you don't need any third party application.Amiamiable
how can I get the selected file ? I guess it's the path, but how ?Tetroxide
@FranciscoCorralesMorales - in onActivityResult()- You can get Uri of the File.Amiamiable
PICKFILE_RESULT_CODE should be PICKFILE_REQUEST_CODE.Chon
@user370305, how to select select docx, doc, rtf, pdf typeHebner
the path is stored in resultIntent.data.path where resultIntent is the intent passed into onActivityResult()Omni
On Samsung S4 with Android 5.0.1 doesn't show anything. Use @alireza answer which works even on Samsung devices.Selfregulating
https://mcmap.net/q/160663/-how-to-get-selected-xls-file-path-from-uri-for-sdk-17-or-below-for-android + github.com/criss721/Android-FileSelectorAlodee
B
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.

Bobbee answered 30/7, 2013 at 14:30 Comment(11)
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.Supralapsarian
The problem is that the intent needs to have mimeType: "file/*", while the Samsung sIntent needs " * / *" - without the spaces between * / *Supralapsarian
where does the string com.sec.android.app.myfiles.PICK_DATA comes from ? which part of it is dynamic ?Tetroxide
Thanks, it's working for me on 4 of my different devices, so it's pretty much universal!Benedictine
What about the other 11k of Android devices.Carolyncarolyne
How to specify the starting location/uri for the file chooser?Jeopardize
How set URI for this intent can you please help me ?Maillol
How to pass the folderpath for this intent, so that FilePicker will open in specific folder directly?Templas
If you want to specify URI you should probably use ACTION_PICK instead of ACTION_GET_CONTENT #17765765Bobbee
In case of Samsung Intent ("com.sec.android.app.myfiles.PICK_DATA") you can try to put String extra "FOLDERPATH" or "START_FOLDER"Bobbee
@Bobbee intent.PutExtra("FOLDERPATH", path); works perfectly. Thank YouTemplas
V
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);

}
Veriee answered 25/6, 2012 at 0:16 Comment(5)
How to convert getting uri into util.File ?Passe
Not sure about getting java.io.File, but can be opened with: developer.android.com/reference/android/content/… How to get simple name is here: https://mcmap.net/q/189913/-contentresolver-how-to-get-file-name-from-uriAnnabelannabela
how to pick a folder?Parham
I like this answer because the type setting allows you to select images as well.Biconcave
in Kotlin use File(data.dataString)Eupepsia
F
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);
    }
Forethoughtful answered 8/1, 2020 at 12:21 Comment(3)
Can you share your onActivityResultCode @Islam KhaledChap
See also org.openintents.action.PICK_FILE for a third option. 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.Allveta
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).Mulatto
S
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.

Septimal answered 23/1, 2012 at 10:41 Comment(4)
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 errorClarhe
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 itClarhe
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.Septimal
How set URI for this intent can you please help me ?Maillol
L
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.

Ligurian answered 3/4, 2014 at 9:33 Comment(0)
D
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.

Ducal answered 15/5, 2020 at 12:42 Comment(0)
M
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

Manysided answered 15/8, 2023 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.