android - 从 SD 卡加载视频缩略图的位图

标签 android performance image-loading video-thumbnails aquery

我正在开发一个模块,我需要在其中以视频缩略图的形式显示手机中的所有视频。我已经使用 BaseAdapter 将所有视频缩略图显示到 GridView 中。唯一的问题是我必须在 BaseAdapter 的 getView() 中编写代码将视频文件中的缩略图提取到位图中。

 ((Activity) context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                 Bitmap bmThumbnail = ThumbnailUtils.createVideoThumbnail(
                                                videoValues.get(position).getFile()
                                               .getAbsolutePath(), Thumbnails.MINI_KIND);
                 imageThumbnail.setImageBitmap(bmThumbnail);
                 bmThumbnail = null;
    }
    });

我想用一些图像加载器异步加载这个。我已经尝试过 Aquery、Universal Image Loader、Picasso 等,但它们都没有提供带有内存缓存、文件缓存、失败回调机制等的异步图像加载。

谁能建议我如何有效地实现这一目标? TIA。

最佳答案

为了解决这个问题,我创建了一个类VideoThumbLoader。它异步生成位图并将其传递给适配器。因此,主要好处是整个过程都在后台线程中处理。

类的代码如下:

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.support.v4.util.LruCache;
import android.widget.ImageView;

public class VideoThumbLoader {

    private LruCache<String, Bitmap> lruCache;

    @SuppressLint("NewApi")
    public VideoThumbLoader() {
        int maxMemory = (int) Runtime.getRuntime().maxMemory();// obtain maximum
                                                                // memory to run
        int maxSize = maxMemory / 4;// get cache memory size 35
        lruCache = new LruCache<String, Bitmap>(maxSize) {
            @Override
            protected int sizeOf(String key, Bitmap value) {
                // this will be called when the cache deposited in each
                return value.getByteCount();
            }
        };
    }

    public void addVideoThumbToCache(String path, Bitmap bitmap) {
        if (getVideoThumbToCache(path) == null && bitmap != null) {

            lruCache.put(path, bitmap);
        }
    }

    public Bitmap getVideoThumbToCache(String path) {

        return lruCache.get(path);
    }

    public void showThumbByAsynctack(String path, ImageView imgview) {

        if (getVideoThumbToCache(path) == null) {
            // asynchronous loading
            new MyBobAsynctack(imgview, path).execute(path);
        } else {
            imgview.setImageBitmap(getVideoThumbToCache(path));
        }
    }

    class MyBobAsynctack extends AsyncTask<String, Void, Bitmap> {
        private ImageView imgView;
        private String path;

        public MyBobAsynctack(ImageView imageView, String path) {
            this.imgView = imageView;
            this.path = path;
        }

        @Override
        protected Bitmap doInBackground(String... params) {

            Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(params[0],
                    MediaStore.Video.Thumbnails.MINI_KIND);

            // provide
            if (getVideoThumbToCache(params[0]) == null) {
                addVideoThumbToCache(path, bitmap);
            }
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (imgView.getTag().equals(path)) {// through Tag can be bound
                                                // pictures address and
                                                // imageView, this address
                                                // Listview loading the image
                                                // dislocation solution
                imgView.setImageBitmap(bitmap);
            }
        }
    }
}

从适配器端,只需调用:

private VideoThumbLoader mVideoThumbLoader = new VideoThumbLoader();
 imageThumbnail.setTag(videoValues.get(position).getFile()
                    .getAbsolutePath());// binding imageview
 imageThumbnail.setImageResource(R.drawable.videothumb); //default image
 mVideoThumbLoader.showThumbByAsynctack(videoValues.get(position)
                    .getFile().getAbsolutePath(), imageThumbnail);

P.S:我注意到的一件重要事情是,由于位图的异步下载,很少有缩略图会混淆的情况。所以我用文件路径标记了 ImageView 。这样我就会得到图像的确切缩略图。

关于android - 从 SD 卡加载视频缩略图的位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35624181/

相关文章:

java - 图像未加载到 jar 文件中

android - 为什么 Android FireMonkey 应用程序中的控件不能跨越多列或多行?

安卓媒体编解码器 : Is it possible to encode audio and video at the same time using mediacodec and muxer?

android - 临时存储的相机 Intent

python - 为什么我的脚本在删除数百万个文件时会定期卡住?

performance - Grails中的交易价格如何?

android - 如果 res 中没有匹配的可绘制对象,Android 如何获取可绘制对象?

android - 从 WP8/WPF 转向 android 开发的建议

html - WebTiming API 中的错误值

android - Flutter CachedNetworkImage 不适用于从 playstore 下载的 App,卡在占位符上