Skip to content

Commit

Permalink
Assume string starting with '/' are file paths.
Browse files Browse the repository at this point in the history
Fixes #161.
  • Loading branch information
sjudd committed Sep 28, 2014
1 parent b51cd25 commit f9f9d2b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class StringLoaderTest {
private StreamStringLoader stringLoader;
private ModelLoader<Uri, InputStream> uriLoader;

@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
uriLoader = mock(ModelLoader.class);
Expand All @@ -46,6 +47,17 @@ public void testHandlesPaths() throws IOException {
verify(uriLoader).getResourceFetcher(eq(Uri.fromFile(f)), eq(IMAGE_SIDE), eq(IMAGE_SIDE));
}

@Test
public void testCanHandleComplexFilePaths() {
assumeTrue(!Util.isWindows());
String testPath = "/storage/emulated/0/DCIM/Camera/IMG_20140520_100001:nopm:.jpg,mimeType=image/jpeg,"
+ "2448x3264,orientation=0,date=Tue";
stringLoader.getResourceFetcher(testPath, IMAGE_SIDE, IMAGE_SIDE);

Uri expected = Uri.fromFile(new File(testPath));
verify(uriLoader).getResourceFetcher(eq(expected), eq(IMAGE_SIDE), eq(IMAGE_SIDE));
}

@Test
public void testHandlesFileUris() throws IOException {
File f = Robolectric.application.getCacheDir();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,21 @@ public StringLoader(ModelLoader<Uri, T> uriLoader) {

@Override
public DataFetcher<T> getResourceFetcher(String model, int width, int height) {
Uri uri = Uri.parse(model);

final String scheme = uri.getScheme();
if (scheme == null) {
uri = Uri.fromFile(new File(model));
Uri uri;
if (model.startsWith("/")) {
uri = toFileUri(model);
} else {
uri = Uri.parse(model);
final String scheme = uri.getScheme();
if (scheme == null) {
uri = toFileUri(model);
}
}

return uriLoader.getResourceFetcher(uri, width, height);
}

private static Uri toFileUri(String path) {
return Uri.fromFile(new File(path));
}
}

0 comments on commit f9f9d2b

Please sign in to comment.