@Before public void setUp() { MockitoAnnotations.initMocks(this); PowerMockito.mockStatic(UriUtil.class, MediaUtils.class); ProducerFactory producerFactory = mock(ProducerFactory.class, RETURNS_MOCKS); mProducerSequenceFactory = new ProducerSequenceFactory( RuntimeEnvironment.application.getContentResolver(), producerFactory, null, true, false, null, false, false, false); when(mImageRequest.getLowestPermittedRequestLevel()) .thenReturn(ImageRequest.RequestLevel.FULL_FETCH); mUri = Uri.parse("http://dummy"); when(mImageRequest.getSourceUri()).thenReturn(mUri); when(MediaUtils.extractMime(mUri.getPath())).thenReturn(mDummyMime); when(MediaUtils.isVideo(mDummyMime)).thenReturn(false); }
@Override public void onRun() throws Throwable { Log.i(TAG, "RUNNING UploadJob for Item["+itemId+"] Uri["+uri.toString()+"]"); if(MediaUtils.isPhoto(UriUtils.extractMimeType(weakReference.get(), uri))) { DataSource<CloseableReference<CloseableImage>> dataSource = Fresco.getImagePipeline().fetchDecodedImage(ImageRequest.fromUri(uri), weakReference.get()); Throwable failureCause = dataSource.getFailureCause(); if (failureCause != null) { throw failureCause; } CloseableReference<CloseableImage> result = dataSource.getResult(); if (result != null) { CloseableImage closeableImage = result.get(); if (closeableImage instanceof CloseableBitmap) { Bitmap bitmap = ((CloseableBitmap) closeableImage).getUnderlyingBitmap(); uploadFile(createImageTempFile(bitmap)); } } }else{ uploadFile(new File(uri.getPath())); } }
/** * This is a utility method which returns the type of Uri * @param uri The Uri to test * @return The type of the given Uri if available or SOURCE_TYPE_UNKNOWN if not */ private static @SourceUriType int getSourceUriType(final Uri uri) { if (uri == null) { return SOURCE_TYPE_UNKNOWN; } if (UriUtil.isNetworkUri(uri)) { return SOURCE_TYPE_NETWORK; } else if (UriUtil.isLocalFileUri(uri)) { if (MediaUtils.isVideo(MediaUtils.extractMime(uri.getPath()))) { return SOURCE_TYPE_LOCAL_VIDEO_FILE; } else { return SOURCE_TYPE_LOCAL_IMAGE_FILE; } } else if (UriUtil.isLocalContentUri(uri)) { return SOURCE_TYPE_LOCAL_CONTENT; } else if (UriUtil.isLocalAssetUri(uri)) { return SOURCE_TYPE_LOCAL_ASSET; } else if (UriUtil.isLocalResourceUri(uri)) { return SOURCE_TYPE_LOCAL_RESOURCE; } else if (UriUtil.isDataUri(uri)) { return SOURCE_TYPE_DATA; } else if (UriUtil.isQualifiedResourceUri(uri)) { return SOURCE_TYPE_QUALIFIED_RESOURCE; } else { return SOURCE_TYPE_UNKNOWN; } }
private Producer<CloseableReference<CloseableImage>> getBasicDecodedImageSequence( ImageRequest imageRequest) { Preconditions.checkNotNull(imageRequest); Uri uri = imageRequest.getSourceUri(); Preconditions.checkNotNull(uri, "Uri is null."); switch (imageRequest.getSourceUriType()) { case SOURCE_TYPE_NETWORK: return getNetworkFetchSequence(); case SOURCE_TYPE_LOCAL_VIDEO_FILE: return getLocalVideoFileFetchSequence(); case SOURCE_TYPE_LOCAL_IMAGE_FILE: return getLocalImageFileFetchSequence(); case SOURCE_TYPE_LOCAL_CONTENT: if (MediaUtils.isVideo(mContentResolver.getType(uri))) { return getLocalVideoFileFetchSequence(); } return getLocalContentUriFetchSequence(); case SOURCE_TYPE_LOCAL_ASSET: return getLocalAssetFetchSequence(); case SOURCE_TYPE_LOCAL_RESOURCE: return getLocalResourceFetchSequence(); case SOURCE_TYPE_QUALIFIED_RESOURCE: return getQualifiedResourceFetchSequence(); case SOURCE_TYPE_DATA: return getDataFetchSequence(); default: throw new IllegalArgumentException( "Unsupported uri scheme! Uri is: " + getShortenedUriString(uri)); } }
public static String extractMimeType(Context context, Uri uri){ File f = new File(uri.getPath()); if(f.isFile()){ return MediaUtils.extractMime(f.getPath()); }else{ return context.getContentResolver().getType(uri); } }
public static boolean isImage(Context c, Uri u){ return MediaUtils.isPhoto(extractMimeType(c, u)); }