Thumb from video url returns null on android 4.0 and above

你。 提交于 2020-01-14 04:49:11

问题


In my application i have to show list of videos,i have created image(thumb) from video and show that image in list.

I added code that i was used for create thumb-

Bitmap bitmap=ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

My problem is that when i run app on android api level below 4.0 thumb is generate but when i run same application on android 4.0 and above ThumbnailUtils.createVideoThumbnail() method returns null. Please help me how to fix this issue.

Working from yesterday but still not getting solution.I have tried -

Bitmap bitmap=ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.MINI_KIND);
and
Bitmap bitmap=ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.MICRO_KIND);

but still returning null.

Thanks in advance.


回答1:


createVideoThumbnail(String filePath, int kind) supports MINI_KIND or MICRO_KIND as kind only.

see http://developer.android.com/reference/android/media/ThumbnailUtils.html.

try one of those... regards

note: May return null if the video is corrupt or the format is not supported.




回答2:


Some devices can't play and can't create thumbnails for videos, that placed on internal memory. Check it, and move your video to SD Card before thumbnails creating.




回答3:


Here is my solution to fix this problem-

Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath,
                MediaStore.Images.Thumbnails.MINI_KIND);

Hope this will fix your problem.




回答4:


If your video located on the external storage requires permission in manifest <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />




回答5:


You can use the following function to get a bitmap from a video URL.

public Bitmap retriveVideoFrameFromVideo(String videoPath){
    Bitmap bitmap = null;

    MediaMetadataRetriever mediaMetadataRetriever = null;
    try {
        mediaMetadataRetriever = new MediaMetadataRetriever();
        if (Build.VERSION.SDK_INT >= 14)
            // no headers included
            mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
        else
            mediaMetadataRetriever.setDataSource(videoPath);
        bitmap = mediaMetadataRetriever.getFrameAtTime();
    } catch (Exception e) {
        e.printStackTrace();

    } finally {
        if (mediaMetadataRetriever != null)
            mediaMetadataRetriever.release();
    }
    return bitmap;
}


来源:https://stackoverflow.com/questions/19048825/thumb-from-video-url-returns-null-on-android-4-0-and-above

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!