android - 是否可以使用 Universal Image Loader 显示视频缩略图,如何显示?

标签 android image video universal-image-loader

我正在尝试使用 Universal Image Loader ( https://github.com/nostra13/Android-Universal-Image-Loader ) 在 GridView 中显示视频缩略图。我能够让它毫无问题地显示图像缩略图。

我如何在 Application 类中初始化 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() 代码异味的原因。

关于android - 是否可以使用 Universal Image Loader 显示视频缩略图,如何显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20931585/

相关文章:

image - 如何解决VB6中的运行时错误 '429 : Activex component can' t创建对象?

html - 在 iOS 应用程序中显示丰富内容的最佳方式是什么?

php - iPhone 中缺少 HTTP_RANGE

html - flowplayer 定位不正确

java - 无法让位置管理器在 Fragment 内工作

android - 将 Google map 应用程序移植到 Osmdroid - 覆盖问题

python - Matplotlib:以图像作为注释的 3D 散点图

android - BufferQueue 已被放弃 : When playing video with TextureView

android - 一些 child 点击后可展开的 ListView 崩溃

android - 如何在android中的strings.xml中的警报对话框中添加项目名称