15
15
*******************************************************************************/
16
16
package com .nostra13 .universalimageloader .core .download ;
17
17
18
+ import android .annotation .TargetApi ;
18
19
import android .content .ContentResolver ;
19
20
import android .content .Context ;
20
21
import android .graphics .Bitmap ;
21
22
import android .graphics .Bitmap .CompressFormat ;
23
+ import android .media .ThumbnailUtils ;
22
24
import android .net .Uri ;
25
+ import android .os .Build ;
23
26
import android .provider .ContactsContract ;
24
27
import android .provider .MediaStore ;
28
+ import android .webkit .MimeTypeMap ;
25
29
import com .nostra13 .universalimageloader .core .DisplayImageOptions ;
26
30
import com .nostra13 .universalimageloader .core .assist .ContentLengthInputStream ;
27
31
import com .nostra13 .universalimageloader .utils .IoUtils ;
@@ -92,8 +96,6 @@ public InputStream getStream(String imageUri, Object extra) throws IOException {
92
96
return getStreamFromAssets (imageUri , extra );
93
97
case DRAWABLE :
94
98
return getStreamFromDrawable (imageUri , extra );
95
- case VIDEO :
96
- return getStreamFromSdCardVideo (imageUri , extra );
97
99
case UNKNOWN :
98
100
default :
99
101
return getStreamFromOtherSource (imageUri , extra );
@@ -159,8 +161,25 @@ protected HttpURLConnection createConnection(String url, Object extra) throws IO
159
161
*/
160
162
protected InputStream getStreamFromFile (String imageUri , Object extra ) throws IOException {
161
163
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 ;
164
183
}
165
184
166
185
/**
@@ -176,7 +195,7 @@ protected InputStream getStreamFromContent(String imageUri, Object extra) throws
176
195
ContentResolver res = context .getContentResolver ();
177
196
178
197
Uri uri = Uri .parse (imageUri );
179
- if (isVideoUri (uri )) { // video thumbnail
198
+ if (isVideoContentUri (uri )) { // video thumbnail
180
199
Long origId = Long .valueOf (uri .getLastPathSegment ());
181
200
Bitmap bitmap = MediaStore .Video .Thumbnails
182
201
.getThumbnail (res , origId , MediaStore .Images .Thumbnails .MINI_KIND , null );
@@ -219,29 +238,7 @@ protected InputStream getStreamFromDrawable(String imageUri, Object extra) {
219
238
int drawableId = Integer .parseInt (drawableIdString );
220
239
return context .getResources ().openRawResource (drawableId );
221
240
}
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
+
245
242
/**
246
243
* Retrieves {@link InputStream} of image by URI from other source with unsupported scheme. Should be overriden by
247
244
* successors to implement image downloading from special sources.<br />
@@ -259,28 +256,14 @@ protected InputStream getStreamFromOtherSource(String imageUri, Object extra) th
259
256
throw new UnsupportedOperationException (String .format (ERROR_UNSUPPORTED_SCHEME , imageUri ));
260
257
}
261
258
262
- private boolean isVideoUri (Uri uri ) {
259
+ private boolean isVideoContentUri (Uri uri ) {
263
260
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/" );
270
262
}
271
- private boolean isSDCardVideoUri (Uri uri ) {
272
- String mimeType = getMimeType (uri .toString ());
273
- if (mimeType == null ) {
274
- return false ;
275
- }
276
263
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/" );
278
268
}
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
- }
286
269
}
0 commit comments