Android 视频缩略图为空

Android video thumbnail is null

基本上我想要实现的是用户可以选择 select 现有视频或拍摄新视频,但我的位图始终为空值。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK || requestCode == SELECT_VIDEO && resultCode == RESULT_OK) {
        Uri selectedVideo = data.getData();
        addVideoToTable(ThumbnailUtils.createVideoThumbnail(selectedVideo.getPath(), MediaStore.Video.Thumbnails.MINI_KIND));
    }
}

public void addVideoToTable(Bitmap video) {
    TableLayout tl = (TableLayout)findViewById(R.id.videoTable);
    if (videoCount == 0 || videoCount % 3 == 0) {
        tr = new TableRow(this);
        tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL));
        tl.addView(tr);
    }
    ImageView iv = new ImageView(this);
    iv.setImageBitmap(video);
    iv.setPadding(0, 0, 10, 10);
    iv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 300, 1f));
    tr.addView(iv);
    videoCount++;
}

尝试:

public String getPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);

    //Source not from device capture or selection
    if (cursor == null) { 
        return uri.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
        if ( idx == -1 ) {
            return uri.getPath();
    }

    String path =  cursor.getString(idx);
    cursor.close();
    return path;
}

当您 select 捕获或存储在用户设备上的本地视频时,这应该适用。它对您不起作用的原因是您得到的只是原始路径,而不是 ThumbnailUtils 方法使用的媒体存储中的路径。