17
17
18
18
import android .content .ContentResolver ;
19
19
import android .content .Context ;
20
+ import android .graphics .Bitmap ;
21
+ import android .graphics .Bitmap .CompressFormat ;
20
22
import android .net .Uri ;
21
- import android .provider .ContactsContract ;
22
-
23
+ import android .provider .MediaStore ;
23
24
import com .nostra13 .universalimageloader .core .DisplayImageOptions ;
24
25
import com .nostra13 .universalimageloader .core .assist .ContentLengthInputStream ;
25
26
import com .nostra13 .universalimageloader .utils .IoUtils ;
26
27
27
28
import java .io .BufferedInputStream ;
29
+ import java .io .ByteArrayInputStream ;
30
+ import java .io .ByteArrayOutputStream ;
28
31
import java .io .File ;
29
32
import java .io .FileInputStream ;
30
33
import java .io .FileNotFoundException ;
39
42
* {@link URLConnection} is used to retrieve image stream from network.
40
43
*
41
44
* @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
42
- * @see HttpClientImageDownloader
43
45
* @since 1.8.0
44
46
*/
45
47
public class BaseImageDownloader implements ImageDownloader {
@@ -57,8 +59,7 @@ public class BaseImageDownloader implements ImageDownloader {
57
59
58
60
protected static final String CONTENT_CONTACTS_URI_PREFIX = "content://com.android.contacts/" ;
59
61
60
- private static final String ERROR_UNSUPPORTED_SCHEME = "UIL doesn't support scheme(protocol) by default [%s]. "
61
- + "You should implement this support yourself (BaseImageDownloader.getStreamFromOtherSource(...))" ;
62
+ private static final String ERROR_UNSUPPORTED_SCHEME = "UIL doesn't support scheme(protocol) by default [%s]. " + "You should implement this support yourself (BaseImageDownloader.getStreamFromOtherSource(...))" ;
62
63
63
64
protected final Context context ;
64
65
protected final int connectTimeout ;
@@ -171,11 +172,20 @@ protected InputStream getStreamFromFile(String imageUri, Object extra) throws IO
171
172
protected InputStream getStreamFromContent (String imageUri , Object extra ) throws FileNotFoundException {
172
173
ContentResolver res = context .getContentResolver ();
173
174
Uri uri = Uri .parse (imageUri );
174
- if (imageUri .startsWith (CONTENT_CONTACTS_URI_PREFIX )) {
175
- return ContactsContract .Contacts .openContactPhotoInputStream (res , uri );
176
- } else {
177
- return res .openInputStream (uri );
175
+
176
+ if (isVideoUri (uri )) {
177
+ Long origId = Long .valueOf (uri .getLastPathSegment ());
178
+ Bitmap bitmap = MediaStore .Video .Thumbnails
179
+ .getThumbnail (res , origId , MediaStore .Images .Thumbnails .MINI_KIND , null );
180
+ if (bitmap != null ) {
181
+ ByteArrayOutputStream bos = new ByteArrayOutputStream ();
182
+ bitmap .compress (CompressFormat .PNG , 0 , bos );
183
+ byte [] bmpData = bos .toByteArray ();
184
+ return new ByteArrayInputStream (bmpData );
185
+ }
178
186
}
187
+
188
+ return res .openInputStream (uri );
179
189
}
180
190
181
191
/**
@@ -222,4 +232,14 @@ protected InputStream getStreamFromDrawable(String imageUri, Object extra) {
222
232
protected InputStream getStreamFromOtherSource (String imageUri , Object extra ) throws IOException {
223
233
throw new UnsupportedOperationException (String .format (ERROR_UNSUPPORTED_SCHEME , imageUri ));
224
234
}
235
+
236
+ private boolean isVideoUri (Uri uri ) {
237
+ String mimeType = context .getContentResolver ().getType (uri );
238
+
239
+ if (mimeType == null ) {
240
+ return false ;
241
+ }
242
+
243
+ return mimeType .startsWith ("video/" );
244
+ }
225
245
}
0 commit comments