0

Google changed the way storage works in Android 10 and 11, i cant get the file path of a file (PDF) while using ACTION_GET_CONTENT. How do i get a file object from a content uri, i am unable to get even a file path from a Content Uri. And if i dont have the file path, i cant get a file object.

This was the method that i was using to get the file path from a content uri.

public String getPDFPath(Uri uri) {

  final String id = DocumentsContract.getDocumentId(uri);
  final Uri contentUri = ContentUris.withAppendedId(
    uri, Long.valueOf(id));

  String[] projection = {
    MediaStore.Images.Media.DATA
  };
  Cursor cursor = getContentResolver().query(contentUri, projection, null, null, null);
  int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  cursor.moveToFirst();
  return cursor.getString(column_index);
}
  • Why do you want a file object/path if you have a nice uri? – blackapps Jun 14 '21 at 10:03
  • You could as well have `final Uri contentUri = uri;`. They are the same now too. – blackapps Jun 14 '21 at 10:06
  • I want to upload the file object to server. Api needs a file object. –  Jun 14 '21 at 10:07
  • Which api? A normal api can upload from an uri. – blackapps Jun 14 '21 at 10:09
  • REST API! I am using Retrofit. You are saying i should send the content uri in the api? How will the backend get the file from the content uri? –  Jun 14 '21 at 10:11
  • 1
    You should upload the file from the uri. The backend is unaware of that as the backend just receives the bytes of the file as always. Wrong question. – blackapps Jun 14 '21 at 10:28
  • Upload the file from Uri? What does it mean? I need a file path for a file. –  Jun 14 '21 at 10:33
  • "I am using Retrofit" -- great! Then you can use one of the `InputStreamRequestBody` implementations to upload using the `Uri`! See the duplicate question, [the OkHttp issue with the original implementation](https://github.com/square/okhttp/issues/3585#issuecomment-327319196), [my blog post with a Kotlin port](https://commonsware.com/blog/2020/07/05/multipart-upload-okttp-uri.html), and [this blog post with a different Kotlin port](https://cketti.de/2020/05/23/content-uris-and-okhttp/). – CommonsWare Jun 14 '21 at 10:39

0 Answers0