Damian
Damian

Reputation: 3050

ContentResolver's query results have strange columns

I want to query for a data of a image or video selected by the user. My onActivityResult queries for DATA field of a image with given URI.

protected final String[] queryColumns = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns._ID, MediaStore.MediaColumns.MIME_TYPE};
[...]
Cursor cursor = getContentResolver().query(uri, queryColumns, null, null, null);
    //uri is content://com.android.providers.media.documents/document/image%3A42026

But DATA column is null, instead when i remove queryColumns form the query I get following columns and values:

0, name: document_id type: 3 ,data: image:42026
1, name: mime_type type: 3 ,data: image/jpeg
2, name: _display_name type: 3 ,data: 20170208_193525_Burst01.jpg
3, name: last_modified type: 1 ,data: 1486578925000
4, name: flags type: 1 ,data: 1
5, name: _size type: 1 ,data: 3246750
6, name: filePath type: 3 ,data: /storage/80BF-9F6E/DCIM/Camera/20170208_193525_Burst01.jpg
7, name: drmType type: 1 ,data: 0
8, name: drmMimeType type: 3 ,data: image/jpeg
9, name: drmContentType type: 1 ,data: 0
10, name: canForward type: 1 ,data: 1
11, name: judgeRight type: 1 ,data: -1

Where this columns come from? Where can I find their definitions?

Code for starting an intent:

  protected void galleryIntent() {
        Intent intent = new Intent();
        intent.setType("image/* video/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);

        if (Build.VERSION.SDK_INT >= 18) {
            intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
            startActivityForResult(Intent.createChooser(intent, "Select some files"), PICK_IMAGE_MULTIPLE);
        } else {
            startActivityForResult(Intent.createChooser(intent, "Select a file"), SELECT_FILE);
        }

    }

Upvotes: 1

Views: 807

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006829

But DATA column is null

That is because your Uri has nothing to do with the MediaStore. You cannot choose randomly-selected columns from randomly-selected providers and think that you can get those for any randomly-selected Uri.

Where this columns come from?

From the com.android.providers.media.documents ContentProvider, as implemented in your version of Android and possibly as modified by your device manufacturer.

Where can I find their definitions?

Most are not part of the Android SDK. The OpenableColumns represent two of those columns.

Code for starting an intent

You are requesting that the user choose a video from any ACTION_GET_CONTENT activity that supports choosing a video. If you add CATEGORY_OPENABLE to your Intent, then you can use ContentResolver and openInputStream() to read in the content identified by the Uri. There is requirement that the Uri be backed by a file that you can read from the filesystem, let alone any reliable way of identifying that file given the Uri.

Upvotes: 2

Related Questions