7

in my app i need to display video thumb nail which is stored in res/raw folder. i searched regard it. i get the following code.

int id = **"The Video's ID"**
ImageView iv = (ImageView ) convertView.findViewById(R.id.imagePreview);
ContentResolver crThumb = getContentResolver();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, id,    MediaStore.Video.Thumbnails.MICRO_KIND, options);
iv.setImageBitmap(curThumb);

by this code i get video thumb nail from sdcard. but video from res/raw folder it does not show video thumb nail. i tried long time but i could not find solution. i tried in the following way.

I create an array and store the resource id (Ex: int[] videoid={R.raw.vi1,...}) and place the id in MediaStore.Video.Thumbnails.getThumbnail(crThumb, videoid[position], MediaStore.Video.Thumbnails.MICRO_KIND, options);.

please help me. thanks in advance.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182

3 Answers3

19

You can try to get some frame out of your video just like this:

Uri  videoURI = Uri.parse("android.resource://" + getPackageName() +"/"
        +R.raw.cashword_video);
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(this, videoURI);
Bitmap bitmap = retriever
        .getFrameAtTime(100000,MediaMetadataRetriever.OPTION_PREVIOUS_SYNC);
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
Serhii Nadolynskyi
  • 5,473
  • 3
  • 21
  • 20
  • This works great! thanks for the pointer. Too many people have stated its not possible without writing the file out to the sdcard... :( – josh527 Oct 22 '13 at 23:33
2

Video on res/raw folder of your application aren't managed by Android Media Content Provider, so you cannot obtain their thumbnail from it.

Alternatively, you can try to save them as normal file in your SD and, then, ask to media provider for thumbnail or create it directly via ThumbnailUtils.createVideoThumbnail class (only Android 2.2 or above)

Rainbowbreeze
  • 1,503
  • 1
  • 9
  • 14
0

Use Android Glide Library for that

In Project Gradle

repositories {
  mavenCentral()
  google()
}

In app Gradle

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.10.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
}

Use below code in you activity

Glide.with(this).load(R.raw.demo).diskCacheStrategy(NONE).into(imageView);
Glide.with(this).load("file:///android_asset/demo.mp4").diskCacheStrategy(NONE).into(imageView);
Glide.with(this).load("android.resource://" + context.getPackageName() + "/raw/demo").diskCacheStrategy(NONE).into(imageView);
Community
  • 1
  • 1
Manthan Patel
  • 1,784
  • 19
  • 23