7heViking
7heViking

Reputation: 7587

Get file path from content URI

I had this working at a time, but now it fails every time I try to get the file path. I am receiving the file from Acrobat Reader and can receive the file name and size, but not the relative file path.

My code looks like this:

if(uri.getScheme().equals("content"))
        {
            String[] dataFields = new String[]{
                    MediaStore.MediaColumns.DATA,
                    OpenableColumns.DISPLAY_NAME,
                    OpenableColumns.SIZE};
        Cursor cursor = context.getContentResolver().query(uri, dataFields, null, null, null);
        cursor.moveToFirst();

        fileLocation = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
        title = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
        size = cursor.getInt(cursor.getColumnIndex(OpenableColumns.SIZE));

        cursor.close();

        contentType = context.getContentResolver().getType(uri);
    }

I get the following error in LogCat:

Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 2 columns.

I understand it as it can't find the column. However, I don't understand how this can be?

Upvotes: 1

Views: 1283

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007534

A Uri is not a file. There is no requirement that the Uri be from MediaStore or otherwise have a MediaStore.MediaColumns.DATA column. If you want to access the content represented by the Uri, use openInputStream() on a ContentResolver.

Upvotes: 4

Related Questions