Android File Picker with real File Path

Hello,

Today I was looking for File Chooser for an android application Iā€™m working on. Unfortunately most of available methods does not return a correct file path to the file.

 public void btnClicked(View view)
    {
        Intent myFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
        myFileIntent.setType("*/*");
        Intent.createChooser(myFileIntent, "Choose a file");
        startActivityForResult(myFileIntent, 10);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        switch (requestCode){
            case 10:
                if(resultCode== RESULT_OK)
                {
                    Uri fileUri = data.getData();

                    String path = FileUtils.getPath(this, fileUri);

                    txtFilePath.setText(path);
                    if(path == null || path.isEmpty())
                        return;

                    File file = new File(path);

                    if(file.exists())
                    {
                        Toast.makeText(this, "File Exist", Toast.LENGTH_SHORT).show();

                    }else{
                        Toast.makeText(this, "Cannot Access it!", Toast.LENGTH_SHORT).show();

                    }
                }

                break;
        }
    }

 

I found this FileUtils.javaĀ  file that really made it easy to access the the real file path of the file.

Hope this helps.

Best of luck

–Saeed šŸ™‚



BTC: 1G1myr8rYv7SgyYtyWXLu3WLSPNaHCGGcd

Comments are closed.