如何在我的/ sdcard / Android / data / mypackage / files文件夹中获取video的缩略图?

查询MediaStore.Video.Media.EXTERNAL_CONTENT_URI只返回/sdcard/DCIM/100MEDIAvideo

但是我想在我的/sdcard/Android/data/mypackage/files夹中获取video的缩略图。 可能吗 ?

这是我的代码的一部分:

  ContentResolver cr = getContentResolver(); String[] proj = { BaseColumns._ID }; Cursor c = cr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null, null, null); if (c.moveToFirst()) { do { int id = c.getInt(0); Bitmap b = MediaStore.Video.Thumbnails.getThumbnail(cr, id, MediaStore.Video.Thumbnails.MINI_KIND, null); Log.d("*****My Thumbnail*****", "onCreate bitmap " + b); ImageView iv = (ImageView) findViewById(R.id.img_thumbnail); iv.setImageBitmap(b); } while( c.moveToNext() ); } c.close(); 

如果您使用的是Android-8(Froyo)或更高版本,则可以使用ThumbnailUtils.createVideoThumbnail :

 Bitmap thumb = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND); 

使用滑动它将获取asynchronous的缩略图。

  Glide.with(context) .load(videoFilePath) // or URI/path .into(imgView); //imageview to set thumbnail to 

您可以使用FFmpegMediaMetadataRetriever并忘记reflection:

 /** * * @param path * the path to the Video * @return a thumbnail of the video or null if retrieving the thumbnail failed. */ public static Bitmap getVideoThumbnail(String path) { Bitmap bitmap = null; FFmpegMediaMetadataRetriever fmmr = new FFmpegMediaMetadataRetriever(); try { fmmr.setDataSource(path); final byte[] data = fmmr.getEmbeddedPicture(); if (data != null) { bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); } if (bitmap == null) { bitmap = fmmr.getFrameAtTime(); } } catch (Exception e) { bitmap = null; } finally { fmmr.release(); } return bitmap; } 
 BitmapFactory.Options options = new BitmapFactory.Options(); options.inDither = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; Bitmap bitmapThumb = MediaStore.Video.Thumbnails.getThumbnail(mActivity.getContentResolver(), Long.parseLong(video_id), Images.Thumbnails.MINI_KIND, options); 

使用选项来加载位图大小的位图

VIDEO_ID获取video缩略图:

 public static Drawable getVideoThumbnail(Context context, int videoID) { try { String[] projection = { MediaStore.Video.Thumbnails.DATA, }; ContentResolver cr = context.getContentResolver(); Cursor cursor = cr.query( MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, projection, MediaStore.Video.Thumbnails.VIDEO_ID + "=?", new String[] { String.valueOf(videoID) }, null); cursor.moveToFirst(); return Drawable.createFromPath(cursor.getString(0)); } catch (Exception e) { } return null; } 

看@Ajji的回答:

  Glide.with(context) .load(videoFilePath) // or URI/path .into(imgView); //imageview to set thumbnail to 

它有时会返回黑色图像,这个问题已经在Glide库的问题中提到

使用此代码:

  BitmapPool bitmapPool = Glide.get(activity).getBitmapPool(); int microSecond = 6000000;// 6th second as an example VideoBitmapDecoder videoBitmapDecoder = new VideoBitmapDecoder(microSecond); FileDescriptorBitmapDecoder fileDescriptorBitmapDecoder = new FileDescriptorBitmapDecoder(videoBitmapDecoder, bitmapPool, DecodeFormat.PREFER_ARGB_8888); Glide.with(activity) .load(videoPath) .asBitmap() .override(50,50)// Example .videoDecoder(fileDescriptorBitmapDecoder) .into(holder.ivFirstUpload); 

这与马修·威利斯(Matthew Willis)有类似的答案,但有更多的反思。 为什么? 因为科学。

 /** * * @param path * the path to the Video * @return a thumbnail of the video or null if retrieving the thumbnail failed. */ public static Bitmap getVidioThumbnail(String path) { Bitmap bitmap = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) { bitmap = ThumbnailUtils.createVideoThumbnail(path, Thumbnails.MICRO_KIND); if (bitmap != null) { return bitmap; } } // MediaMetadataRetriever is available on API Level 8 but is hidden until API Level 10 Class<?> clazz = null; Object instance = null; try { clazz = Class.forName("android.media.MediaMetadataRetriever"); instance = clazz.newInstance(); final Method method = clazz.getMethod("setDataSource", String.class); method.invoke(instance, path); // The method name changes between API Level 9 and 10. if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD) { bitmap = (Bitmap) clazz.getMethod("captureFrame").invoke(instance); } else { final byte[] data = (byte[]) clazz.getMethod("getEmbeddedPicture").invoke(instance); if (data != null) { bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); } if (bitmap == null) { bitmap = (Bitmap) clazz.getMethod("getFrameAtTime").invoke(instance); } } } catch (Exception e) { bitmap = null; } finally { try { if (instance != null) { clazz.getMethod("release").invoke(instance); } } catch (final Exception ignored) { } } return bitmap; } 

如果您直接创build缩略图,如下所示

 Bitmap thumb = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND); 

如果您正在为大video集创build缩略图(对于大量video),则此方法存在问题。 应用程序将冻结,直到所有的缩略图都被加载,因为所有的进程都在主线程中执行。

使用SuziLoader

这个加载器将加载本地存储在文件系统中的video的缩略图。

 String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/video.mp4"; ImageView mThumbnail = (ImageView) findViewById(R.id.thumbnail); SuziLoader loader = new SuziLoader(); //Create it for once loader.with(MainActivity.this) //Context .load(path) //Video path .into(mThumbnail) // imageview to load the thumbnail .type("mini") // mini or micro .show(); // to show the thumbnail 

要获得这种依赖关系,请使用以下步骤

第1步。将JitPack存储库添加到您的构build文件
将其添加到您的根build.gradle存储库的末尾:

 allprojects { repositories { ... maven { url 'https://jitpack.io' } } } 

第2步。添加依赖关系

 dependencies { compile 'com.github.sushinpv:SuziVideoThumbnailLoader:0.1.0' } 

添加读取外部存储清单中的权限

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>