0

I am trying to get the filename and filepath from uri

the uri is content://com.android.providers.media.documents/document/audio%3A180

I am trying:

Uri filePathUri = uri;
if (uri.getScheme().toString().compareTo("content")==0)
{
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
    if (cursor.moveToFirst())
    {
        int column_index = 
        cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        filePathUri = Uri.parse(cursor.getString(column_index));
        fileName = filePathUri.getLastPathSegment().toString();
    }
}

I am getting an error at cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);

java.lang.IllegalArgumentException: column '_data' does not exist

How to get the filename

Santhosh
  • 9,965
  • 20
  • 103
  • 243
  • See also [this](https://stackoverflow.com/q/48510584/115145), [this](https://stackoverflow.com/q/35870825/115145), and [this](https://commonsware.com/blog/2016/03/15/how-consume-content-uri.html). – CommonsWare May 10 '18 at 11:10
  • but the file is there in external storage. – Santhosh May 10 '18 at 11:14
  • did you try to follow those 3 links? the last one describes your problem in depth, imho – pskink May 10 '18 at 11:41
  • 1
    If you only want to work with files, stop using `ACTION_OPEN_DOCUMENT` and use [a file picker library](https://android-arsenal.com/tag/35?sort=created). Otherwise, whether *you* happened to pick a file does not matter, because *other people* might not pick a file. They might pick something from Google Drive, or from a Windows file server. The Storage Access Framework is an abstraction around storage, and as such, you should not be assuming that a `Uri` that you get from `ACTION_OPEN_DOCUMENT` has anything to do with a file on the filesystem that you can access. – CommonsWare May 10 '18 at 11:48

0 Answers0