无法从ThumbnailUtils.createVideoThumbnail

时间:2018-04-20 11:52:31

标签: java android

我试图从我从设备的图库或相机中获取的视频中获取位图,但是当我调用它时,我得到一个空位图:

@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);
    }

我也试过使用它,但它仍然返回一个空值:

String path = uri.getPath(); // -> /document/video:76
Bitmap thumbnailVideo = ThumbnailUtils.createVideoThumbnail(path , MediaStore.Video.Thumbnails.MICRO_KIND);

我尝试的最后一件事是这段代码,但和以前一样,它不起作用:

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);

我做错了什么?我尝试了Stackoverflow的不同解决方案,但似乎没有一个适合我。

1 个答案:

答案 0 :(得分:0)

我找到了一种方法来完成这项工作。

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;
}