How to open a file browser in Android?
Asked Answered
J

4

14

I am developing on a Android 4.0.3 device. How do I open a file browser for my app? Is there one built in the to Android SDK? Do I have to write my own?

I don't want my app to depend on a the user installing a separate app for file browsing.

Janae answered 30/1, 2013 at 16:2 Comment(0)
N
22

To get a file from a file browser, use this:

        Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT);
        fileintent.setType("gagt/sdf");
        try {
            startActivityForResult(fileintent, PICKFILE_RESULT_CODE);
        } catch (ActivityNotFoundException e) {
            Log.e("tag", "No activity can handle picking a file. Showing alternatives.");
        }

I'm not quite sure what the gagt/sdf is for... it seems to work in my app for any file.

Then add this method:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Fix no activity available
        if (data == null)
            return;
        switch (requestCode) {
        case PICKFILE_RESULT_CODE:
            if (resultCode == RESULT_OK) {
                String FilePath = data.getData().getPath();
                //FilePath is your file as a string
            }
        }

If the user doesn't have a file manager app installed or preinstalled by their OEM you're going to have to implement your own. You might as well give them a choice.

Neapolitan answered 30/1, 2013 at 16:43 Comment(0)
E
5

I hope this one will help you for file picking:

public void performFileSearch() {

    // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
    // browser.
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

    // Filter to only show results that can be "opened", such as a
    // file (as opposed to a list of contacts or timezones)
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    // Filter to show only images, using the image MIME data type.
    // If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
    // To search for all documents available via installed storage providers,
    // it would be "*/*".
    intent.setType("image/*");

    startActivityForResult(intent, READ_REQUEST_CODE);
}

The code is from this documentation:
https://developer.android.com/guide/topics/providers/document-provider.html
Refer to it for more information.

Erund answered 17/3, 2018 at 5:8 Comment(0)
R
2

If someone would still need it for newer versions, it got updated with developing Storage Access Framework by Google for SDK >= 4.4. Check this Google site for further details:

https://developer.android.com/guide/topics/providers/document-provider.html

Rinarinaldi answered 17/2, 2017 at 8:24 Comment(0)
S
0

There is no single file-management app that is installed across all devices. You probably want your app to be also working on devices with Android 3.x or lower. The best choice you have though is writing your own file-manager. It isn't as much effort as it might sound, there is a lot of code on this already out there on the web.

Swamper answered 30/1, 2013 at 16:5 Comment(2)
maybe i need to work on my googling skills but i am having trouble finding an easy to follow code example...Janae
madteam.co/news/2011/10/android-diy-making-a-file-manager and #4109381 should provide an entry pointSwamper

© 2022 - 2024 — McMap. All rights reserved.