提交 f9f9d2ba 编写于 作者: S Sam Judd

Assume string starting with '/' are file paths.

Fixes #161.
上级 b51cd256
......@@ -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);
......@@ -46,6 +47,17 @@ public class StringLoaderTest {
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();
......
......@@ -20,12 +20,21 @@ public class StringLoader<T> implements ModelLoader<String, T> {
@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));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册