I have a video and I need get its thumbnail. I use ThumbnailUtils
class for this purpose, but it returns null instead of expected Bitmap
. When I debug my app, I see that inside createVideoThumbnail
method MediaMetadataRetriever.setDataSource
invokes and it throws IllegalArgumentException
. Inside setDataSource
I see this code:
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();
}
}
I think it means that file with given file doesn't exist. But I see it in file system and it plays fine. Maybe there's another reason?
The question is what you're passing to that method and from what you're obtaining it?
Below is the way I am doing this, and this works for me, so it should work for you as well.
To get selectedVideoPath you should use https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java#L257
getPath(Context context, Uri uri)
method. Originally it looked like:Additionally you can add checking if videoFile exists by calling videoFile.exists().
Additionally if you're trying to read from external storage you can check if you have included READ_EXTERNAL_STORAGE permission in Manifest file, and if you're saving file and trying to get it thumbnail you should also need WRITE_EXTERNAL_STORAGE permission.