我有一个视频,我需要获取它的缩略图。我为此目的使用ThumbnailUtils
类,但它返回null而不是预期的Bitmap
。当我调试我的应用程序时,我看到createVideoThumbnail
方法MediaMetadataRetriever.setDataSource
内部调用并抛出IllegalArgumentException
。在setDataSource
内,我看到了这段代码:
public void setDataSource(String path) throws IllegalArgumentException {
if (path == null) {
throw new IllegalArgumentException();
}
try (FileInputStream is = new FileInputStream(path)) {
FileDescriptor fd = is.getFD();
setDataSource(fd, 0, 0x7ffffffffffffffL);
} catch (FileNotFoundException fileEx) {
throw new IllegalArgumentException(); // and I've found out that method throws IllegalArgimentException inside this catch block
} catch (IOException ioEx) {
throw new IllegalArgumentException();
}
}
我认为这意味着给定文件的文件不存在。但我在文件系统中看到它并且它播放正常。也许还有其他原因?
答案 0 :(得分:3)
问题是您传递给该方法的内容以及您获取该方法的内容是什么?
以下是我这样做的方式,这对我有用,所以它也适合你。
File videoFile = new File(selectedVideoPath);
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(videoFile.getAbsolutePath(), MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);
要获取 selectedVideoPath ,您应该使用https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java#L257 getPath(Context context, Uri uri)
方法。最初它看起来像:
public static String getPath(final Context context, final Uri uri) {
if (DEBUG)
Log.d(TAG + " File -",
"Authority: " + uri.getAuthority() +
", Fragment: " + uri.getFragment() +
", Port: " + uri.getPort() +
", Query: " + uri.getQuery() +
", Scheme: " + uri.getScheme() +
", Host: " + uri.getHost() +
", Segments: " + uri.getPathSegments().toString()
);
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// LocalStorageProvider
if (isLocalStorageDocument(uri)) {
// The path is the id
return DocumentsContract.getDocumentId(uri);
}
// ExternalStorageProvider
else if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
此外,您可以通过调用 videoFile.exists()来添加检查 videoFile 是否存在。
此外,如果您尝试从外部存储中读取数据,则可以检查您是否在清单文件中包含 READ_EXTERNAL_STORAGE 权限,以及您是否正在保存文件并尝试获取该文件缩略图您还需要 WRITE_EXTERNAL_STORAGE 权限。
答案 1 :(得分:0)
创建视频缩略图时,null背后有两个原因。视频已损坏或 ThumbnailUtils 不支持该格式。
仔细检查您的文件格式,并使用下面的缩略图代码
// Create VideoThumbnail from sdcard path like this.
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath, Thumbnails.MINI_KIND);