Dan*_*iel 5 video android image universal-image-loader
我正在尝试使用Universal Image Loader(https://github.com/nostra13/Android-Universal-Image-Loader)在网格视图中显示视频缩略图.我能够让它显示图像缩略图没有问题.
我如何在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);
}
Run Code Online (Sandbox Code Playgroud)
我如何用它来显示缩略图:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
为简单起见,省略了一些代码.contentUri可以是图像URI或视频URI,在两种情况下都是形式content://...
是否可以使用此库从视频内容URI中显示视频缩略图?怎么样?
Dan*_*iel 10
好的,我明白了.ImageLoaderConfiguration有一个选项,您可以传入图像解码器.
这是我改变初始化的方式:
ImageDecoder smartUriDecoder = new SmartUriDecoder(getContentResolver(), new BaseImageDecoder(false));
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.taskExecutor(ThreadPool.getExecutorService())
.defaultDisplayImageOptions(displayOptions)
.imageDecoder(smartUriDecoder)
.build();
Run Code Online (Sandbox Code Playgroud)
和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+$", "");
}
}
Run Code Online (Sandbox Code Playgroud)
在UIL的文档中,它表示info.getImageKey()将返回为此图像指定的原始URI,但最后添加了附加大小,我找不到获取原始URI的方法.因此,cleanUriString()代码闻起来的原因.
| 归档时间: |
|
| 查看次数: |
4192 次 |
| 最近记录: |