Skip to content

Commit fe2fd9f

Browse files
committed
Video file thumbnail support
1 parent fbf2e63 commit fe2fd9f

File tree

2 files changed

+32
-49
lines changed

2 files changed

+32
-49
lines changed

library/src/com/nostra13/universalimageloader/core/download/BaseImageDownloader.java

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
*******************************************************************************/
1616
package com.nostra13.universalimageloader.core.download;
1717

18+
import android.annotation.TargetApi;
1819
import android.content.ContentResolver;
1920
import android.content.Context;
2021
import android.graphics.Bitmap;
2122
import android.graphics.Bitmap.CompressFormat;
23+
import android.media.ThumbnailUtils;
2224
import android.net.Uri;
25+
import android.os.Build;
2326
import android.provider.ContactsContract;
2427
import android.provider.MediaStore;
28+
import android.webkit.MimeTypeMap;
2529
import com.nostra13.universalimageloader.core.DisplayImageOptions;
2630
import com.nostra13.universalimageloader.core.assist.ContentLengthInputStream;
2731
import com.nostra13.universalimageloader.utils.IoUtils;
@@ -92,8 +96,6 @@ public InputStream getStream(String imageUri, Object extra) throws IOException {
9296
return getStreamFromAssets(imageUri, extra);
9397
case DRAWABLE:
9498
return getStreamFromDrawable(imageUri, extra);
95-
case VIDEO:
96-
return getStreamFromSdCardVideo(imageUri, extra);
9799
case UNKNOWN:
98100
default:
99101
return getStreamFromOtherSource(imageUri, extra);
@@ -159,8 +161,25 @@ protected HttpURLConnection createConnection(String url, Object extra) throws IO
159161
*/
160162
protected InputStream getStreamFromFile(String imageUri, Object extra) throws IOException {
161163
String filePath = Scheme.FILE.crop(imageUri);
162-
return new ContentLengthInputStream(new BufferedInputStream(new FileInputStream(filePath), BUFFER_SIZE),
163-
(int) new File(filePath).length());
164+
if (isVideoFileUri(imageUri)) {
165+
return getVideoThumbnailStream(filePath);
166+
} else {
167+
BufferedInputStream imageStream = new BufferedInputStream(new FileInputStream(filePath), BUFFER_SIZE);
168+
return new ContentLengthInputStream(imageStream, (int) new File(filePath).length());
169+
}
170+
}
171+
172+
@TargetApi(Build.VERSION_CODES.FROYO)
173+
private InputStream getVideoThumbnailStream(String filePath) {
174+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
175+
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Images.Thumbnails.FULL_SCREEN_KIND);
176+
if (bitmap != null) {
177+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
178+
bitmap.compress(CompressFormat.PNG, 0, bos);
179+
return new ByteArrayInputStream(bos.toByteArray());
180+
}
181+
}
182+
return null;
164183
}
165184

166185
/**
@@ -176,7 +195,7 @@ protected InputStream getStreamFromContent(String imageUri, Object extra) throws
176195
ContentResolver res = context.getContentResolver();
177196

178197
Uri uri = Uri.parse(imageUri);
179-
if (isVideoUri(uri)) { // video thumbnail
198+
if (isVideoContentUri(uri)) { // video thumbnail
180199
Long origId = Long.valueOf(uri.getLastPathSegment());
181200
Bitmap bitmap = MediaStore.Video.Thumbnails
182201
.getThumbnail(res, origId, MediaStore.Images.Thumbnails.MINI_KIND, null);
@@ -219,29 +238,7 @@ protected InputStream getStreamFromDrawable(String imageUri, Object extra) {
219238
int drawableId = Integer.parseInt(drawableIdString);
220239
return context.getResources().openRawResource(drawableId);
221240
}
222-
/**
223-
* Retrieves {@link InputStream} of video thumbnail by URI (video is located on the local file system or SD card).
224-
*
225-
* @param imageUri Image URI
226-
* @param extra Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object)
227-
* DisplayImageOptions.extraForDownloader(Object)}; can be null
228-
* @return {@link InputStream} of video thumbnail
229-
* @throws IOException if some I/O error occurs reading from file system
230-
*/
231-
protected InputStream getStreamFromSdCardVideo(String imageUri, Object extra) throws FileNotFoundException {
232-
imageUri=imageUri.replace("video://", "");
233-
Uri uri = Uri.parse(imageUri);
234-
if (isSDCardVideoUri(uri)) { // video thumbnail
235-
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(imageUri,
236-
MediaStore.Images.Thumbnails.FULL_SCREEN_KIND);
237-
if (bitmap != null) {
238-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
239-
bitmap.compress(CompressFormat.PNG, 0, bos);
240-
return new ByteArrayInputStream(bos.toByteArray());
241-
}
242-
}
243-
return null;
244-
}
241+
245242
/**
246243
* Retrieves {@link InputStream} of image by URI from other source with unsupported scheme. Should be overriden by
247244
* successors to implement image downloading from special sources.<br />
@@ -259,28 +256,14 @@ protected InputStream getStreamFromOtherSource(String imageUri, Object extra) th
259256
throw new UnsupportedOperationException(String.format(ERROR_UNSUPPORTED_SCHEME, imageUri));
260257
}
261258

262-
private boolean isVideoUri(Uri uri) {
259+
private boolean isVideoContentUri(Uri uri) {
263260
String mimeType = context.getContentResolver().getType(uri);
264-
265-
if (mimeType == null) {
266-
return false;
267-
}
268-
269-
return mimeType.startsWith("video/");
261+
return mimeType != null && mimeType.startsWith("video/");
270262
}
271-
private boolean isSDCardVideoUri(Uri uri) {
272-
String mimeType = getMimeType(uri.toString());
273-
if (mimeType == null) {
274-
return false;
275-
}
276263

277-
return mimeType.startsWith("video/");
264+
private boolean isVideoFileUri(String uri) {
265+
String extension = MimeTypeMap.getFileExtensionFromUrl(uri);
266+
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
267+
return mimeType != null && mimeType.startsWith("video/");
278268
}
279-
public static String getMimeType(String url)
280-
{
281-
String extension = url.substring(url.lastIndexOf("."));
282-
String mimeTypeMap = MimeTypeMap.getFileExtensionFromUrl(extension);
283-
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(mimeTypeMap);
284-
return mimeType;
285-
}
286269
}

library/src/com/nostra13/universalimageloader/core/download/ImageDownloader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public interface ImageDownloader {
4343

4444
/** Represents supported schemes(protocols) of URI. Provides convenient methods for work with schemes and URIs. */
4545
public enum Scheme {
46-
HTTP("http"), HTTPS("https"), FILE("file"), CONTENT("content"), ASSETS("assets"), DRAWABLE("drawable"), VIDEO("video"), UNKNOWN("");
46+
HTTP("http"), HTTPS("https"), FILE("file"), CONTENT("content"), ASSETS("assets"), DRAWABLE("drawable"), UNKNOWN("");
4747

4848
private String scheme;
4949
private String uriPrefix;

0 commit comments

Comments
 (0)