I'm trying to get a Bitmap from a video which I get from the device's gallery or the camera, but when I call this, I'm getting a null Bitmap:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri uri = data.getData();
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
Uri videoUri = data.getData(); // -> content://com.android.providers.media.documents/document/video%3A76
// This returns null
Bitmap thumbnailVideo = ThumbnailUtils.createVideoThumbnail(videoUri.toString(), MediaStore.Video.Thumbnails.MICRO_KIND);
media1.setImageBitmap(thumbnailVideo);
}
I also tried using this, but it stills returns a null value:
String path = uri.getPath(); // -> /document/video:76
Bitmap thumbnailVideo = ThumbnailUtils.createVideoThumbnail(path , MediaStore.Video.Thumbnails.MICRO_KIND);
And the last thing I tried is this code, but as before, it doesn't work:
String[] filePathColumn = {MediaStore.Video.Media.DATA};
Cursor cursor = this.getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap thumbnailVideo = ThumbnailUtils.createVideoThumbnail(picturePath, MediaStore.Video.Thumbnails.MICRO_KIND);
What am I doing wrong? I tried different solutions from Stackoverflow but none of them seem to work for me.
I found a way to make this work.
try {
Bitmap thumbnailVideo;
if (Build.VERSION.SDK_INT < 19) {
thumbnailVideo = ThumbnailUtils.createVideoThumbnail(getRealPathFromURIForVideoAPI18(videoUri), MediaStore.Video.Thumbnails.MICRO_KIND);
}
else {
thumbnailVideo = ThumbnailUtils.createVideoThumbnail(getRealPathFromURIForVideoAPI19(videoUri), MediaStore.Video.Thumbnails.MICRO_KIND);
}
setThumbnail(thumbnailVideo);
} catch (Exception ex) {
ex.printStackTrace();
}
// API 18 or less (Android 4.3)
@SuppressLint("NewApi")
public String getRealPathFromURIForVideoAPI18(Uri contentUri) {
String[] proj = {MediaStore.Images.Media.DATA};
String result = null;
CursorLoader cursorLoader = new CursorLoader(this, contentUri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
cursor.close();
}
return result;
}
// API 19 or higher (>Android 4.4 Kitkat)
@SuppressLint("NewApi")
private String getRealPathFromURIForVideoAPI19(Uri selectedVideoUri) {
String wholeID = DocumentsContract.getDocumentId(selectedVideoUri);
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Video.Media.DATA };
String sel = MediaStore.Video.Media._ID + "=?";
Cursor cursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{ id }, null);
String filePath = "";
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.