Simple Android Directory picker - How?

前端 未结 5 1882
执念已碎
执念已碎 2021-02-05 07:03

I have just started coding in Android Studio and feeling Awesome..!!

How can I write a code for a \'Directory Picker\'. i.e., When a button is clicked, a simple Dialog/

5条回答
  •  长情又很酷
    2021-02-05 07:56

    Try to use Intent.ACTION_OPEN_DOCUMENT_TREE

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){ 
        Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); 
        i.addCategory(Intent.CATEGORY_DEFAULT);
        startActivityForResult(Intent.createChooser(i, "Choose directory"), 9999);
    }
    

    And get the result Uri from onActivityResult data.getData()

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        switch(requestCode) {
            case 9999:
                Log.i("Test", "Result URI " + data.getData());
                break;
        }
    }
    

提交回复
热议问题