獲取 Android raw 目錄下 視訊的縮圖
阿新 • • 發佈:2019-01-06
以下程式碼用於獲取 android raw目錄下視訊檔案的縮圖:
/** * 獲取 raw 下 指定視訊檔案的縮圖 * @param aVideoName * @return */ private Drawable getThumbnail(String aVideoName) { Drawable ret = null; int id = 0; try { id = R.raw.class.getDeclaredField(aVideoName).getInt(this); Uri videoURI = Uri.parse("android.resource://"+ this.getPackageName() + "/" + id); ret = getThumbnail(videoURI); } catch ( IllegalAccessException aE ) { aE.printStackTrace(); } catch ( NoSuchFieldException aE ) { aE.printStackTrace(); } return ret; } private Drawable getThumbnail(Uri aVideoUri) { MediaMetadataRetriever retriever = newMediaMetadataRetriever(); retriever.setDataSource(this, aVideoUri); Bitmap bitmap = retriever .getFrameAtTime(1*1000*1000, MediaMetadataRetriever.OPTION_PREVIOUS_SYNC); Drawable drawable = new BitmapDrawable(getResources(), bitmap); return drawable; }