Android: Directory and file chooser android library

南楼画角 提交于 2019-11-30 09:11:03

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/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!