Android ThumbnailUtils 返回 null,filenotfoundexception?
Android ThumbnailUtils returning null, filenotfoundexception?
当前正在尝试从返回的视频(从画廊或从设备上的视频 Intent)中获取缩略图以显示在 ImageView 中,但遇到 FileNotFound 异常并返回空位图。不完全确定我哪里出错了,尽管根据错误判断它一定是路径不正确。有什么想法吗?
if (requestCode == GALLERY_VIDEO_CODE || requestCode == VIDEO_CODE) {
videoURI = data.getData();
if(videoURI != null) {
MediaPlayer mp = MediaPlayer.create(this, videoURI);
int duration = mp.getDuration();
mp.release();
if (duration <= 11000) {
addBtn.setVisibility(View.INVISIBLE);
clearBtn.setVisibility(View.VISIBLE);
Toast.makeText(this, "Video added!", Toast.LENGTH_LONG).show();
File file = new File(videoURI.getPath());
Bitmap bm = ThumbnailUtils.createVideoThumbnail(file.getAbsolutePath(), MediaStore.Video.Thumbnails.MINI_KIND);
userPhoto.setImageBitmap(bm);
} else {
videoURI = null;
Toast.makeText(this, "Video too long - maximum duration is 10 seconds", Toast.LENGTH_LONG).show();
addBtn.setVisibility(View.VISIBLE);
clearBtn.setVisibility(View.INVISIBLE);
}
通过使用以下方法将我的文件路径放入 createVideoThumbnail() 方法,稍微解决了这个问题:
当前正在尝试从返回的视频(从画廊或从设备上的视频 Intent)中获取缩略图以显示在 ImageView 中,但遇到 FileNotFound 异常并返回空位图。不完全确定我哪里出错了,尽管根据错误判断它一定是路径不正确。有什么想法吗?
if (requestCode == GALLERY_VIDEO_CODE || requestCode == VIDEO_CODE) {
videoURI = data.getData();
if(videoURI != null) {
MediaPlayer mp = MediaPlayer.create(this, videoURI);
int duration = mp.getDuration();
mp.release();
if (duration <= 11000) {
addBtn.setVisibility(View.INVISIBLE);
clearBtn.setVisibility(View.VISIBLE);
Toast.makeText(this, "Video added!", Toast.LENGTH_LONG).show();
File file = new File(videoURI.getPath());
Bitmap bm = ThumbnailUtils.createVideoThumbnail(file.getAbsolutePath(), MediaStore.Video.Thumbnails.MINI_KIND);
userPhoto.setImageBitmap(bm);
} else {
videoURI = null;
Toast.makeText(this, "Video too long - maximum duration is 10 seconds", Toast.LENGTH_LONG).show();
addBtn.setVisibility(View.VISIBLE);
clearBtn.setVisibility(View.INVISIBLE);
}
通过使用以下方法将我的文件路径放入 createVideoThumbnail() 方法,稍微解决了这个问题: