7

I need to get real path of pdf file the user choosed. Here is my code:

Button click:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
startActivityForResult(intent, 2);

onActivityResult:

Uri uri = data.getData();
String picturePath = uri.toString();

here is the solution for images,Get filename and path from URI from mediastore, however it does not work for pdf, because there used MediaStore.Images.Media.DATA.

Community
  • 1
  • 1
sinitram
  • 524
  • 5
  • 14
  • Please tell what you see if you Toast or Log `uri.getPath()`. And which application was used by the intent? – greenapps Apr 22 '15 at 13:17
  • content://com.android.externalstorage.documents/document/1213-181F%3ADownload%2F2%20%D0%BA%D1%83%D1%80%D1%81_4%20%D0%BC%D0%BE%D0%B4%D1%83%D0%BB%D1%8C%20%D0%9F%D0%98_%D0%B1%D0%B0%D0%BA_2014_2015.pdf Which application? – sinitram Apr 22 '15 at 13:24
  • Quite strange i must say. You did not tell which app the user used to pick the file. Can you tell what the user saw as filename while picking the file? Try other apps like ES File Explorer. You will see quite different paths. – greenapps Apr 22 '15 at 13:27
  • there are latters in russian. 2 курс_4 модуль ПИ_бак_2014_2015.pdf – sinitram Apr 22 '15 at 13:29
  • O nice you are so kind to tell that. Now first try with latin filenames please. And which app was used to pick the file? – greenapps Apr 22 '15 at 13:30
  • Oh, I do not know wich app was used, it is default android picker new Intent(Intent.ACTION_GET_CONTENT); – sinitram Apr 22 '15 at 13:34
  • After that intent the user can chooce an app first. I can choose from Recent, Downloads, Documents To Go and ES File Explorer. Every app delivers the picked file different. So try all. Try ES File Explorer. I made a file with cyrillic characters in the filename and it displayes ok using ES File Explorer. Please show your code where you Toast or Log uri.getPath(). – greenapps Apr 22 '15 at 13:39
  • Try `Toast.makeText(getBaseContext(), uri.getPath(), Toast.LENGTH_LONG).show();`. You really see those %B4 chars? Or cyrillic? – greenapps Apr 22 '15 at 13:40
  • Aha Documents to go showed these %2D's too.But it show sthe file path. So a little conversion will do i think. ES File Explorer sdelivers the file path already. – greenapps Apr 22 '15 at 13:44
  • To pick file used Downloads. Here is the sample of toast with latin latters. content://com.android.externalstorage.documents/document/1213-181F%3ADownload%2FResume.pdf Here are code. Uri uri = data.getData(); String picturePath = uri.toString(); Toast.makeText(this, picturePath, Toast.LENGTH_LONG).show(); – sinitram Apr 22 '15 at 13:44
  • In toast I do not see cyrillic, just %2D – sinitram Apr 22 '15 at 13:46
  • Install ES File Explorer! Now did you already? – greenapps Apr 22 '15 at 13:47
  • I've installed ES FE and toast text is the same. – sinitram Apr 22 '15 at 14:01
  • Which one of the solutions to get the real path (from the link you posted) did you try? All? – greenapps Apr 22 '15 at 14:23
  • 1
    Yes, I'v tried all. I know hot ot get the file correct name "resume.pdf" name, but I can not get the path. – sinitram Apr 22 '15 at 14:27
  • So ES File Explorer delivers with uri.getPath() something that starts with content://docu... ? – greenapps Apr 22 '15 at 14:32
  • what do you mean by "real path" ? – Heshan Sandeepa Apr 22 '15 at 15:41
  • Heshan, for example real path for content://com.android.externalstorage.documents/document/1213-181F%3ADownload%2F‌ is /sdcard/Download/my Report.pdf – sinitram Apr 22 '15 at 16:39
  • I changed the language of one of my devices to russian. ES Проводник behaved like before. – greenapps Apr 22 '15 at 17:47
  • @sinitram did you get the solution, i am facing the same problem – Parth Anjaria Mar 07 '16 at 10:42
  • @ParthAnjaria unfortunately, no( Then I just used images. – sinitram Mar 07 '16 at 11:18
  • i need for files, can anyone help me? – Parth Anjaria Mar 08 '16 at 04:26
  • This worked for me: http://stackoverflow.com/a/27926504/1009684 – Philipp E. Oct 17 '16 at 11:56

0 Answers0