0

I'm developing a feature which takes .pdf, .doc and .docx file by using ACTION_GET_CONTENT Intent.

You probably guessed that i need to get Real file path from the uri returned by Intent. I have searched for atleast 3 days to find out code which gives file or real path by using URI. But in those code, only files selected from either Downloads or Recent or internal or some other is working. But couldn't find code which can work for all options viz, Recent, Downloads, Internal storage, SD Card, Drive and Drop box.

These are the solutions i tried.. https://www.dev2qa.com/how-to-get-real-file-path-from-android-uri/

How to get the Full file path from URI

and even this too,

https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java

I want code which works in android versions between Android Lollipop and Android Pie.

This is my Intent

Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT);
intent1.addCategory(Intent.CATEGORY_OPENABLE);
intent1.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
if (mimeTypes.length > 0) {
    intent1.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
}
startActivityForResult(Intent.createChooser(intent1, "ChooseFile"), REQUEST_CODE_PICK_FILE);

And everytime i test these codes,

Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1024, result=-1, data=Intent { dat=content://com.android.externalstorage.documents/document/primary:Download/62-2.pdf flg=0x1 }} to activity {com.xxx.xxx.xxx}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Syed Zeeshan
  • 490
  • 1
  • 7
  • 13
  • See also: https://stackoverflow.com/q/49221312/115145 and https://stackoverflow.com/q/35870825/115145. In short, there is no "real path", as `ACTION_GET_CONTENT` does not have to return a file, let alone one that you can access via the filesystem. – CommonsWare Jan 28 '19 at 12:27
  • Thanks.. i found that not all files have path. So I have got the Display file name of that file using uri and got the content of file by using `contentResolver` and `openInputStream` and saved its content to a new file stored in app data. By this way, i could able to get file from whatever source. – Syed Zeeshan Jan 31 '19 at 03:16

0 Answers0