Permalink
Browse files

Provide canonical way to create ImageRequest for local files

Summary:
This change adds a canonical method `ImageRequest#fromFile` in order to allow developers not having to care about the correct way to create URIs for local files.

Closes #1596

Reviewed By: oprisnik

Differential Revision: D4386923

fbshipit-source-id: e2edc8f31b7dd2ea7698a5b1bba9fcd47da67923
  • Loading branch information...
1 parent 1d4461d commit c6dcedcfbdd94ee26d2911bdfcef30f812551010 @lambdapioneer lambdapioneer committed with facebook-github-bot Jan 10, 2017
@@ -11,6 +11,8 @@
import javax.annotation.Nullable;
+import java.io.File;
+
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
@@ -187,6 +189,16 @@ public static String getRealPathFromUri(ContentResolver contentResolver, final U
}
/**
+ * Returns a URI for a given file using {@link Uri#fromFile(File)}.
+ *
+ * @param file a file with a valid path
+ * @return the URI
+ */
+ public static Uri getUriForFile(File file) {
+ return Uri.fromFile(file);
+ }
+
+ /**
* Return a URI for the given resource ID.
* The returned URI consists of a {@link #LOCAL_RESOURCE_SCHEME} scheme and
* the resource ID as path.
@@ -17,6 +17,7 @@
import android.net.Uri;
import com.facebook.common.internal.Objects;
+import com.facebook.common.util.UriUtil;
import com.facebook.imagepipeline.common.ImageDecodeOptions;
import com.facebook.imagepipeline.common.Priority;
import com.facebook.imagepipeline.common.ResizeOptions;
@@ -71,6 +72,10 @@
/** Request listener to use for this image request */
private final @Nullable RequestListener mRequestListener;
+ public static ImageRequest fromFile(@Nullable File file) {
+ return (file == null) ? null : ImageRequest.fromUri(UriUtil.getUriForFile(file));
+ }
+
public static ImageRequest fromUri(@Nullable Uri uri) {
return (uri == null) ? null : ImageRequestBuilder.newBuilderWithSource(uri).build();
}
@@ -9,6 +9,8 @@
package com.facebook.imagepipeline.request;
+import java.io.File;
+
import android.net.Uri;
import com.facebook.imagepipeline.common.ImageDecodeOptionsBuilder;
@@ -52,4 +54,28 @@ public String getName() {
assertThat(copy).isEqualTo(original);
}
+
+ @Test
+ public void testImageRequestForLocalFile_normal() {
+ final File file = new File("/foo/photos/penguin.jpg");
+ final ImageRequest imageRequest = ImageRequest.fromFile(file);
+
+ assertThat(imageRequest.getSourceFile()).isEqualTo(file);
+ }
+
+ @Test
+ public void testImageRequestForLocalFile_withSpaces() {
+ final File file = new File("/foo/photos folder/penguin crowd.jpg");
+ final ImageRequest imageRequest = ImageRequest.fromFile(file);
+
+ assertThat(imageRequest.getSourceFile()).isEqualTo(file);
+ }
+
+ @Test
+ public void testImageRequestForLocalFile_withSpecialCharacters() {
+ final File file = new File("/foo/photos#folder/with spaces/penguin?_&*\\...jpg");
+ final ImageRequest imageRequest = ImageRequest.fromFile(file);
+
+ assertThat(imageRequest.getSourceFile()).isEqualTo(file);
+ }
}

0 comments on commit c6dcedc

Please sign in to comment.