Android视频缩略图为空

时间:2015-06-01 08:29:50

标签: android bitmap thumbnails video-thumbnails

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

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

1 个答案:

答案 0 :(得分:1)

尝试:

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

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