Simple Android Directory picker - How?

前端 未结 5 1817
执念已碎
执念已碎 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:46

    Use below code to select directory

            Intent result = new Intent();
            result.putExtra("chosenDir", path);
            setResult(RESULT_OK, result);
    

    And to get the selected path override onActivityResult :

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == PICK_DIRECTORY && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            String path = (String) extras.get("chosenDir");
    
        }
    }
    

提交回复
热议问题