1
private static final int SELECT_VIDEO = 2346;

public void chooseVideo(View view) {
    Intent videoPickerIntent = new Intent(Intent.ACTION_PICK);
    videoPickerIntent.setType("video/*");
    startActivityForResult(videoPickerIntent, SELECT_VIDEO);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SELECT_VIDEO && resultCode == RESULT_OK) {
        Uri video = data.getData();
    }
}

The uriString of video is content://com.google.android.apps.photos.contentprovider/-1/2/content%3A%2F%2Fmedia%2Fexternal%2Fvideo%2Fmedia%2F141694/ACTUAL/1441715445

Path: /-1/2/content%3A%2F%2Fmedia%2Fexternal%2Fvideo%2Fmedia%2F141694/ACTUAL/1441715445

ContentUris.parseId(video) gives 1441715445 where as I believe I'm looking to get 141694

It's kind of double encoded it seems with this /ACTUAL/... on the end confusing matters.

Is the fault in my understanding or code or with the gallery activity?

Related, but no clean solution given: Choosing photo using new Google Photos app is broken

What I need the id for:

I want the id just so I can get the thumbnail:

long id = ContentUris.parseId(video);
Bitmap thumb = MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(), id,
                  MediaStore.Video.Thumbnails.MICRO_KIND, null);
Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203
  • "I believe I'm looking to get 141694" -- why? What exactly are you expecting to be able to do with that number? There is no requirement for a `Uri` to have any sort of number that you can use for any particular purpose. – CommonsWare Sep 21 '15 at 17:03
  • @CommonsWare I want the `id`, so I can get thumbnail: `Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, id, MediaStore.Video.Thumbnails.MICRO_KIND, null);` – weston Sep 21 '15 at 17:05
  • There is no requirement that the video returned by `ACTION_PICK` have a thumbnail in the `MediaStore`. `ACTION_PICK` can be satisfied by any number of activities from any number of apps. – CommonsWare Sep 21 '15 at 17:06
  • @CommonsWare OK, but when it does have a thumb I'd like to display it. What would you do, ignore this route and generate a new thumb perhaps? – weston Sep 21 '15 at 17:08
  • That would be the safest approach. – CommonsWare Sep 21 '15 at 17:08
  • @CommonsWare With [ThumbnailUtils](http://developer.android.com/reference/android/media/ThumbnailUtils.html)? – weston Sep 21 '15 at 17:09
  • Ummm... you can try that. Just realized that it takes a "file path", which is disconcerting, as it may or may not be able to handle a `content://` "path". But that's what I was thinking of. Android, on the whole, wants you to use `Uri` abstractions, then seems to want file paths everywhere. :-( – CommonsWare Sep 21 '15 at 17:13
  • @CommonsWare Yeah I just hit that road block. Erm, maybe I can get a shot from a `MediaPlayer` instance – weston Sep 21 '15 at 17:16

2 Answers2

0

Hacky solution I feel, and you must be prepared for it to return an invalid id as CommonsWare says, there is no guarantee it is in the Uri to begin with.

public static long getId(Uri uri) {
    String path = uri.getPath();
    if ("com.google.android.apps.photos.contentprovider".equals(uri.getAuthority())) {
        int iActual = path.indexOf("ACTUAL");
        if (iActual > -1) {
            String subPath = path.substring(0, iActual - 1);
            uri = Uri.parse(subPath);
        }
    }
    return ContentUris.parseId(uri);
}

Usage to get thumb (which can be null):

@Nullable
public static Bitmap getThumbnail(Context context, Uri videoUri) {
    long id = getId(videoUri);
    return MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(),
            id, MediaStore.Video.Thumbnails.MINI_KIND, null);
}
weston
  • 54,145
  • 21
  • 145
  • 203
  • did you try to get all the data associated with that `videoUri` ? see `MediaStore.Video.VideoColumns` for all the columns you can access – pskink Sep 21 '15 at 19:10
  • it seems that not all columns from `MediaStore.Video.VideoColumns` are supported: i couldn't find a way for getting `BaseColumns#_COUNT` field and its documentation is, hmmm, non-existing... – pskink Sep 22 '15 at 09:29
  • @pskink Thanks, I did try a query first to get the id. it returned nothing, so I enumerated all the videos and that's actually how I discovered that the id was in the uri already. – weston Sep 22 '15 at 11:24
  • for a quick preview call: `DatabaseUtils.dumpCursor(MediaStore.Video.query(getContentResolver(), videoUri, null));` – pskink Sep 22 '15 at 11:34
  • @pskink I'll give it another go, but as far as I remember that was a query I tried that returned no rows. – weston Sep 22 '15 at 12:00
  • it should return exactly one row: the row describing your video, check it out, note **_id** column – pskink Sep 22 '15 at 12:17
0

@pskink comment was right, you can get info using a cursor. Using the dumpCursor function you can see the available info in the log:

Dumping cursor android.content.ContentResolver$CursorWrapperInner@461ff77
0 {
    _id=47        
    _display_name=ddmsrec.mp4
    _size=4324395
    mime_type=video/mp4
    _data=/storage/emulated/0/ddmsrec.mp4
    orientation=0
    datetaken=1481123284000
    latitude=null
    longitude=null
    special_type_id=null
}

That cursor is the info for this URI, so you can see that the _id column is the right value:

URI content://com.google.android.apps.photos.contentprovider/-1/2/content%3A%2F%2Fmedia%2Fexternal%2Ffile%2F47/ORIGINAL/NONE/864252053
Community
  • 1
  • 1
RominaV
  • 3,335
  • 1
  • 29
  • 59