Android 是否可以使用通用图像加载器显示视频缩略图,以及如何显示?

Android 是否可以使用通用图像加载器显示视频缩略图,以及如何显示?,android,image,video,universal-image-loader,Android,Image,Video,Universal Image Loader,我正在尝试使用Universal Image Loader()在网格视图中显示视频缩略图。我能够让它显示图像缩略图没有问题 如何在应用程序类中初始化UIL: @Override public void onCreate() { super.onCreate(); initUil(); } private void initUil() { DisplayImageOptions displayOptions = new DisplayImageOptions.Builde

我正在尝试使用Universal Image Loader()在网格视图中显示视频缩略图。我能够让它显示图像缩略图没有问题

如何在应用程序类中初始化UIL:

@Override
public void onCreate() {
    super.onCreate();
    initUil();
}

private void initUil() {
    DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .cacheOnDisc(true)
            .build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .taskExecutor(ThreadPool.getExecutorService())
            .defaultDisplayImageOptions(displayOptions)
            .build();

    ImageLoader.getInstance().init(config);
}
如何使用它显示缩略图:

public class MediaCursorAdapter extends SimpleCursorAdapter implements Filterable {
    @Override
    public void bindView(View rowView, Context context, Cursor cursor) {
        String contentUri = getContentUri(cursor);

        ImageView imgThumb = (ImageView) rowView.findViewById(R.id.imgThumb);

        ImageLoader.getInstance().displayImage(contentUri, imgThumb);
    }
}
为了简单起见,省略了一些代码
contentUri
可以是图像URI或视频URI,在这两种情况下都是
content://...


是否可以使用此库显示视频内容URI中的视频缩略图?怎么做?

我想看看这个答案,这个人似乎解决了问题,但它没有使用通用图像加载器


好的,我想好了
ImageLoaderConfiguration
有一个选项,您可以在其中传入图像解码器

下面是我如何更改初始化的:

ImageDecoder smartUriDecoder = new SmartUriDecoder(getContentResolver(), new BaseImageDecoder(false));

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .taskExecutor(ThreadPool.getExecutorService())
            .defaultDisplayImageOptions(displayOptions)
            .imageDecoder(smartUriDecoder)
            .build();
SmartUriDecoder
类:

public class SmartUriDecoder implements ImageDecoder {
    private final ContentResolver m_contentResolver;
    private final BaseImageDecoder m_imageUriDecoder;

    public SmartUriDecoder(ContentResolver contentResolver, BaseImageDecoder imageUriDecoder) {
        if (imageUriDecoder == null) {
            throw new NullPointerException("Image decoder can't be null");
        }

        m_contentResolver = contentResolver;
        m_imageUriDecoder = imageUriDecoder;
    }

    @Override
    public Bitmap decode(ImageDecodingInfo info) throws IOException {
        if (TextUtils.isEmpty(info.getImageKey())) {
            return null;
        }

        String cleanedUriString = cleanUriString(info.getImageKey());
        Uri uri = Uri.parse(cleanedUriString);
        if (isVideoUri(uri)) {
            return makeVideoThumbnail(info.getTargetSize().getWidth(), info.getTargetSize().getHeight(), getVideoFilePath(uri));
        }
        else {
            return m_imageUriDecoder.decode(info);
        }
    }

    private Bitmap makeVideoThumbnail(int width, int height, String filePath) {
        if (filePath == null) {
            return null;
        }
        Bitmap thumbnail = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Video.Thumbnails.MINI_KIND);
        Bitmap scaledThumb = scaleBitmap(thumbnail, width, height);
        thumbnail.recycle();
        return scaledThumb;
    }

    private boolean isVideoUri(Uri uri) {
        String mimeType = m_contentResolver.getType(uri);
        return mimeType.startsWith("video/");
    }

    private String getVideoFilePath(Uri uri) {
        String columnName = MediaStore.Video.VideoColumns.DATA;
        Cursor cursor = m_contentResolver.query(uri, new String[] { columnName }, null, null, null);
        try {
            int dataIndex = cursor.getColumnIndex(columnName);
            if (dataIndex != -1 && cursor.moveToFirst()) {
                return cursor.getString(dataIndex);
            }
        }
        finally {
            cursor.close();
        }
        return null;
    }

    private Bitmap scaleBitmap(Bitmap origBitmap, int width, int height) {
        float scale = Math.min(
                ((float)width) / ((float)origBitmap.getWidth()),
                ((float)height) / ((float)origBitmap.getHeight())
        );
        return Bitmap.createScaledBitmap(origBitmap,
                (int)(((float)origBitmap.getWidth()) * scale),
                (int)(((float)origBitmap.getHeight()) * scale),
                false
        );
    }

    private String cleanUriString(String contentUriWithAppendedSize) {
        // replace the size at the end of the URI with an empty string.
        // the URI will be in the form "content://....._256x256
        return contentUriWithAppendedSize.replaceFirst("_\\d+x\\d+$", "");
    }
}

在UIL的文档中,它说
info.getImageKey()
将返回为此图像指定的原始URI,但最后附加了一个大小,我找不到获取原始URI的方法。因此,
cleanUriString()
的代码味道就有了原因。

我知道如何生成视频缩略图,但我正在寻找一个带有缓存和同步的可配置解决方案,UIL似乎是一个完美的候选者。请注意,我尝试过这种方法,但我注意到,当我为一堆媒体项目生成缩略图时,速度大大减慢。看起来UIL正在数据目录中为视频本身创建一个缓存项,这意味着大量的处理时间和大文件(看到50MB的文件等)。我仍在关注它,但感谢您为将vids与uil集成提供了一个良好的开端。我也注意到了这一点,但这只是第一次发生。我认为这是因为视频的缩略图不在
MediaStore
中,所以调用
ThumbnailUtils.createVideoThumbnail()
强制创建所有视频缩略图。但是我不知道这个方法是如何工作的,所以我可能在这里完全错了。嗨,我试着根据你的工作制作我的自定义ImageDecoder,但是当我尝试使用它时,解码器失败了,错误是:E/ImageLoader﹕ null java.lang.NullPointerException?有什么线索吗?你应该看看stacktrace,看看它到底在哪里失败。@DanielGabriel:对不起,看起来你的智能解码器可以支持非常好的视频,但无法加载照片
ImageDecoder
尝试加载照片时发生异常。请告诉我们,智能解码器需要采用哪些方法?