Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a file browser in Android?

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.

like image 458
user1451495 Avatar asked Jan 30 '13 16:01

user1451495


People also ask

Where is the file browser in Android?

From the Home screen, tap the Apps icon (in the QuickTap bar) > the Apps tab (if necessary) > Tools folder > File Manager .

How do I open a file app on Android?

On your Android 10 device, open the app drawer and tap the icon for Files. By default, the app displays your most recent files. Swipe down the screen to view all your recent files (Figure A). To see only specific types of files, tap one of the categories at the top, such as Images, Videos, Audio, or Documents.


1 Answers

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.

like image 151
Mgamerz Avatar answered Oct 06 '22 03:10

Mgamerz