user1048121
user1048121

Reputation: 33

MediaStore.Video.Thumbnails.getThumbnail() returns null

I have the following code;

String[] projection = { MediaStore.Video.VideoColumns._ID, MediaStore.Video.VideoColumns.DATA };
                //String selection = MediaStore.Video.VideoColumns.KIND + "=" + MediaStore.Video.VideoColumns.;
                String sort = MediaStore.Video.VideoColumns._ID + " DESC";

                Cursor myCursor = getActivity().managedQuery(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, sort);


                try
                {
                    myCursor.moveToFirst();
                    Log.d("VIDEO_URI", intent.getDataString());
                    Bitmap bitmap = MediaStore.Video.Thumbnails.getThumbnail(
                            getActivity().getContentResolver(), myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns._ID)),
                            MediaStore.Video.Thumbnails.MICRO_KIND,
                            (BitmapFactory.Options) null );

                    ImageView iv = (ImageView) getActivity().findViewById(R.id.attached_media_image);

                    //Log.d("IMAGE", curThumb.toString());

                    iv.setImageBitmap(bitmap);

                }
                catch (Exception e) {
                    // TODO: handle exception
                    Log.e("ERROR", e.toString());
                }

I get the following in my LogCat when this is ran;

03-13 12:05:14.740: D/VIDEO_URI(7269): content://media/external/video/media/474

But it doesn't set the image of the iv to anything, and when I try to run the Log.d("IMAGE", curThumb.toString()); line, that is commented out in this example, it throws a null exception.

Any help would be much appreciated thank you.

Upvotes: 3

Views: 8584

Answers (1)

zapl
zapl

Reputation: 63955

The thumbnail generation is not guaranteed to succeed since the part that generates thumbnails cannot do so for some video codecs. The returned Bitmap will be null in that case.

  • Have you checked if bitmap != null?
  • Do you get a thumbnail for the Video in e.g. the Gallery app?
  • What is curThumb, I can see no definition of that in your code.

Edit: Your error is that you use the ID from the thumbnails table not the id of the video. All you need to do is to get the Bitmap this way:

        Bitmap bitmap = MediaStore.Video.Thumbnails.getThumbnail(
                getActivity().getContentResolver(),
                ContentUris.parseId(intent.getData()),
                MediaStore.Video.Thumbnails.MICRO_KIND,
                (BitmapFactory.Options) null );

Api Doc says: "origId: Original image id associated with thumbnail of interest."

Upvotes: 3

Related Questions