Search code examples
androidbitmapthumbnailsvideo-thumbnails

Android video thumbnail is null


So basically what I'm trying to achieve is that the user can choose to select an existing video or take a new one, but my bitmap has always a null value.

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

Solution

  • Try:

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

    This should work for when you select a local video that is either capture, or stored on a user's device. The reason it isn't working for you is that you are getting just the raw path, rather than the path in the media store that the method ThumbnailUtils uses.