6

I am desperately trying to send a captured video to a server. The problem is that the URI that is given by the built-in camera application is not the real file path. It looks like this - /content:/media/external/video/media/19.

How can I access the real path or the data directly from this kind of URIs?

After reading the android documentation I saw that it looks like a content provider's notation, but I still don't have a clue how to reach the data that I need. Please help!!!

thanks in advance

maddob
  • 989
  • 1
  • 12
  • 29
  • This question is answered there please check https://stackoverflow.com/a/52641312/6086086 – Asad Oct 04 '18 at 07:32
  • This question is answered here please check https://stackoverflow.com/a/52641312/6086086 – Asad Oct 04 '18 at 07:34

2 Answers2

4
public String getRealPathFromURI(Context context, Uri contentUri) {
  Cursor cursor = null;
  try { 
    String[] proj = { MediaStore.Images.Media.DATA };
    cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
  } finally {
    if (cursor != null) {
      cursor.close();
    }
  }
}

see following post for Get filename and path from URI from mediastore

Community
  • 1
  • 1
R_K
  • 803
  • 1
  • 7
  • 18
4

How can I access the real path or the data directly from this kind of URIs?

You don't. It might not exist as a file. Or, it might not exist as a file that you can read except via the ContentProvider.

Instead, use a ContentResolver to open an InputStream on that Uri, and use that InputStream to transfer the data to a server.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Just to show you an example - this is my code: `InputStream is = getContentResolver().openInputStream(Uri.parse(YOUR_URI_STRING)));` – maddob Mar 27 '12 at 21:47
  • How to get type of such URIs? I mean how to know its pdf or ppt or even wav or mp3 file? – JRC May 28 '12 at 10:56
  • @JRC: If you look in the upper-right corner of your Web page, you will see an "Ask Question" button. Please use that, rather than hijacking other questions. That being said, ask the `MediaStore` for the `MIME_TYPE` of your content. – CommonsWare May 28 '12 at 10:59