从sdcard

时间:2016-02-25 10:09:10

标签: 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。

2 个答案:

答案 0 :(得分:1)

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

课程代码如下:

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。这样我就可以得到图像的确切缩略图。

答案 1 :(得分:0)


使用SuziLoader

此加载程序将加载视频的缩略图,这些视频本地存储在后台文件系统中。

String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/video.mp4";
ImageView mThumbnail = (ImageView) findViewById(R.id.thumbnail);

SuziLoader loader = new SuziLoader(); //Create it for once.
loader.with(MainActivity.this) //Context
    .load(path) //Video path
    .into(mThumbnail) // imageview to load the thumbnail
    .type("mini") // mini or micro
    .show(); // to show the thumbnail
  

要获得此依赖关系,请使用以下步骤

第1步。将JitPack存储库添加到构建文件中 将其添加到存储库末尾的根build.gradle中:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

第2步。添加依赖项

dependencies {
    compile 'com.github.sushinpv:SuziVideoThumbnailLoader:0.1.0'
}

添加READ外部存储清单

中的权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>