3

I am trying to put selected files(not exclusive to images, it can be any file) from file chooser intent to a zip file. I need full file path to do this, but intent only gives uri paths.

  1. I have tried .getPath() but that does not give the real path of the file
  2. I have tried getRealPathFromRealURI: android get real path by Uri.getPath()
  3. I have tried File file = new File(), file.getPath()

This is my code:

    public void onActivityResult(int requestCode, int resultCode, Intent result){
        if(requestCode == 111) {
            if(null != result) { // checking empty selection
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    if(null != result.getClipData()) { // checking multiple selection or not
                        for(int i = 0; i < result.getClipData().getItemCount(); i++) {
                            String uri = result.getClipData().getItemAt(i).getUri().getPath();
                            uriList.add(uri);
                            Log.d("PATH: ",uri);
                        }
                        confirmationDialog();
                    } else {
                        String uri = result.getData().getPath();
                        uriList.add(uri);
                        Log.d("PATH: ",uri);
                        confirmationDialog();
                    }
                }else{Toast.makeText(getApplicationContext(),
                        "An error has occured: API level requirements not met",Toast.LENGTH_SHORT).show();};
            }
        }
    }

It should give the real path for example: "/sdcard/filename.example"

Instead, it gives me: "/document/9016-4ef8:filename.example"

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Lex F.
  • 51
  • 1
  • 6

3 Answers3

4
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);

cursor.close();
System.out.println("picturePath +"+ picturePath );  //path of sdcard

Found here: Get Real Path For Uri Android

Samuel Adorni
  • 208
  • 2
  • 14
4

Pick / Get the file's actual path:

val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "*/*"
startActivityForResult(intent, 1)

onActivityResult:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
        val file = data?.data?.let {
            getFileFromUri(requireContext().contentResolver, uri, requireContext().cacheDir)
        }
    }
}

Get File:

private fun getFileFromUri(contentResolver: ContentResolver, uri: Uri, directory: File): File {
    val file =
        File.createTempFile("suffix", ".prefix", directory)
    file.outputStream().use {
        contentResolver.openInputStream(uri)?.copyTo(it)
    }

    return file
}

Once we get the file, We can get the actual path of the file.

0

Okay, I fixed it by using another file explorer aside from the built-in file explorer, in my case I used Cx File Explorer, different file explorers return different values.

Lex F.
  • 51
  • 1
  • 6