0

我的代码,

public static Bitmap retriveVideoFrameFromVideo(String videoPath) throws Throwable {

    Bitmap bitmap = null;
    MediaMetadataRetriever mediaMetadataRetriever = null;
    try {
        mediaMetadataRetriever = new MediaMetadataRetriever();
        if (Build.VERSION.SDK_INT >= 14)
            mediaMetadataRetriever.setDataSource(videoPath, new HashMap<String, String>());
        else
            mediaMetadataRetriever.setDataSource(videoPath);
        //   mediaMetadataRetriever.setDataSource(videoPath);
        bitmap = mediaMetadataRetriever.getFrameAtTime();
    } catch (Exception e) {
        e.printStackTrace();
        throw new Throwable(
                "Exception in retriveVideoFrameFromVideo(String videoPath)"
                        + e.getMessage());

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

这是创建缩略图,但我用了很多时间ListView然后ListView挂断。

4

1 回答 1

2

您需要在 Async 方法中运行此任务,如果您正在使用,则像这样,onBindViewHolder()或者RecycleView如果getView()您正在使用,请使用ListView

 new AsyncTask<String, String, String>() {
            Bitmap bitmapVideo;

            @Override
            protected String doInBackground(String... strings) {
                try {
                   //Your method call here
                    bitmapVideo =retriveVideoFrameFromVideo(strings[0]);
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(String id) {
                super.onPostExecute(id);
                if (bitmapVideo != null) {
                  //Load your bitmap here
                    holder.imgVideoThumb.setImageBitmap(bitmapVideo);
                }
            }
        }.execute(getYourVideolink());

为了提高效率,您将位图图像保存在本地,并且在调用检查天气之前,如果该图像比从本地加载并且没有新的再次AsyncTask()运行,则该图像已经保存在本地AsyncTask()

于 2017-03-03T05:56:02.827 回答