Android: Directory and file chooser android library
Asked Answered
H

1

11

I'm using aFileChooser android library project in my app to select the file from external storage. but it doesn't seem to pick only directory to let user select the download location to download the files. Is there any android library project which support both pick file and pick directory?

I understand there are multiple questions have been answered here either for file chooser or directory chooser but after extensive search I couldn't find one for both directory and file chooser. Any help would be appreciated.

Helgahelge answered 24/3, 2014 at 19:53 Comment(0)
R
15

I have no android library project, but you can simply make your own file chooser with the next code. This code will ask you to chose a file browser, when you select a file in the file browser you'll get the path in the onActivityResult function in the FilePath String.

Create this public:

private static final int ACTIVITY_CHOOSE_FILE = 3;

When a button is clicked you can call this:

            Intent chooseFile;
            Intent intent;
            chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
            chooseFile.setType("file/*");
            intent = Intent.createChooser(chooseFile, "Choose a file");
            startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);

You can catch the directory with this code :

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) return;
        String path     = "";
        if(requestCode == ACTIVITY_CHOOSE_FILE)
        {
              Uri uri = data.getData();
              String FilePath = getRealPathFromURI(uri);

        }
    }

public String getRealPathFromURI(Uri contentUri) {
    String [] proj      = {MediaStore.Images.Media.DATA};
    Cursor cursor       = getContentResolver().query( contentUri, proj, null, null,null); 
    if (cursor == null) return null; 
    int column_index    = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

Edit: If you do not want to use an external file browser, you can import this android library into your project: https://code.google.com/p/afiledialog/

Rijeka answered 24/3, 2014 at 20:7 Comment(5)
Thanks DeGoosseZ for your code... but if user doesnt have a file browser?Helgahelge
When there is no file browser this method wont work. Right now I have no idea of an android library project that can do this. I'll do some research on it.Rijeka
I have not tested this one yet but it seems to have a nice 'how to': code.google.com/p/afiledialogRijeka
yes, that seems good... i will try it out. thanks a lot DeGoosseZHelgahelge
Mark it as an answer for others please. You're welcome!Rijeka

© 2022 - 2024 — McMap. All rights reserved.