0

I am trying to get the full path of a file with i pick up with android browser. Therefore, i use the following code:

private void showFileChooser() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);

        try {
            startActivityForResult(
                    Intent.createChooser(intent, "Select a file"), FILE_SELECT_CODE);
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(this, "You don't have a browser installed", Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==FILE_SELECT_CODE && resultCode==Activity.RESULT_OK) {

                    // Get the Uri of the selected file
                    Uri uri = data.getData();
                    Log.d(TAG, "URI del archivo a enviar: " + uri.toString());
                    // Get the path
                    final String path=Environment.getExternalStorageDirectory().toString()+uri.getPath();
                    Log.d(TAG, "Path del archivo a enviar: " +path);
                    File archivoPruebaParaSaberSiExiste = new File(path);


                    if(archivoPruebaParaSaberSiExiste.exists()) {
                        Log.d(TAG,"File exists");
                    else{
                        Log.d(TAG,"File does not exist");
                    }

        }
        super.onActivityResult(requestCode, resultCode, data);
    }


 <uses-permission android:name="android.permission.READ_PHONE_STATE" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

However, i don´t get the file path:

The file path which i get is: 
      /storage/emulated/0/data/external/images/media/1949

The actual path is: 
     /storage/emulated/0/DCIM/Camera/20165481_133918.jpg

I don´t know what is happening.

MM Manuel
  • 175
  • 1
  • 2
  • 14

1 Answers1

0

I am trying to get the full path of a file with i pick up with android browser.

You are not picking a file. You are picking a piece of content. There is no requirement that this be a file.

However, i don´t get the file path

Again, the chosen content does not have to be a file.

Based on your result, the scheme of your Uri is content, not file. This is perfectly normal and will be the dominant form of Uri in the future. To consume a content Uri, use a ContentResolver and methods like openInputStream().

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • Thank you so much, it was son helpful! – MM Manuel Apr 21 '16 at 09:06
  • @CommansWare Android R preview is released (documenting file path access). I have asked a new question, can you please have a look - https://stackoverflow.com/questions/60360368/android-11-r-file-path-access – HB. Feb 24 '20 at 04:14
  • @HB.: Yeah, I saw it when you posted it (though I forgot to up-vote it, which I did just now). I'm hoping to have an answer for you in the coming weeks. – CommonsWare Feb 24 '20 at 12:10
  • @CommonsWare Thank you. I'm looking forward to hearing from you. – HB. Feb 24 '20 at 12:20