From 716f24ed1098a12c158e0a95a5c3afbfda97086c Mon Sep 17 00:00:00 2001 From: Eric Samelson Date: Thu, 30 Apr 2020 11:20:28 -0700 Subject: [PATCH 1/2] [file-system][android] add ability to read from bundle resources --- .../modules/filesystem/FileSystemModule.java | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/packages/expo-file-system/android/src/main/java/expo/modules/filesystem/FileSystemModule.java b/packages/expo-file-system/android/src/main/java/expo/modules/filesystem/FileSystemModule.java index ba369d1edc694..f4c47f910f2a7 100644 --- a/packages/expo-file-system/android/src/main/java/expo/modules/filesystem/FileSystemModule.java +++ b/packages/expo-file-system/android/src/main/java/expo/modules/filesystem/FileSystemModule.java @@ -165,6 +165,10 @@ private EnumSet permissionsForUri(Uri uri) { if ("file".equals(uri.getScheme())) { return permissionsForPath(uri.getPath()); } + if (uri.getScheme() == null) { + // this is probably an asset embedded by the packager in resources + return EnumSet.of(Permission.READ); + } return EnumSet.noneOf(Permission.class); } @@ -193,6 +197,18 @@ private InputStream openAssetInputStream(Uri uri) throws IOException { return getContext().getAssets().open(asset); } + private InputStream openResourceInputStream(String resourceName) throws IOException { + int resourceId = getContext().getResources().getIdentifier(resourceName, "raw", getContext().getPackageName()); + if (resourceId == 0) { + // this resource doesn't exist in the raw folder, so try drawable + resourceId = getContext().getResources().getIdentifier(resourceName, "drawable", getContext().getPackageName()); + if (resourceId == 0) { + throw new FileNotFoundException("No resource found with the name " + resourceName); + } + } + return getContext().getResources().openRawResource(resourceId); + } + @ExpoMethod public void getInfoAsync(String uriStr, Map options, Promise promise) { try { @@ -216,12 +232,17 @@ public void getInfoAsync(String uriStr, Map options, Promise pro result.putBoolean("isDirectory", false); promise.resolve(result); } - } else if ("content".equals(uri.getScheme()) || "asset".equals(uri.getScheme())) { + } else if ("content".equals(uri.getScheme()) || "asset".equals(uri.getScheme()) || uri.getScheme() == null) { Bundle result = new Bundle(); try { - InputStream is = "content".equals(uri.getScheme()) ? - getContext().getContentResolver().openInputStream(uri) : - openAssetInputStream(uri); + InputStream is; + if ("content".equals(uri.getScheme())) { + is = getContext().getContentResolver().openInputStream(uri); + } else if ("asset".equals(uri.getScheme())) { + is = openAssetInputStream(uri); + } else { + is = openResourceInputStream(uriStr); + } if (is == null) { throw new FileNotFoundException(); } @@ -288,6 +309,9 @@ public void readAsStringAsync(String uriStr, Map options, Promis contents = IOUtils.toString(new FileInputStream(uriToFile(uri))); } else if ("asset".equals(uri.getScheme())) { contents = IOUtils.toString(openAssetInputStream(uri)); + } else if (uri.getScheme() == null) { + // this is probably an asset embedded by the packager in resources + contents = IOUtils.toString(openResourceInputStream(uriStr)); } else { throw new IOException("Unsupported scheme for location '" + uri + "'."); } @@ -434,6 +458,12 @@ public void copyAsync(Map options, Promise promise) { OutputStream out = new FileOutputStream(uriToFile(toUri)); IOUtils.copy(in, out); promise.resolve(null); + } else if (fromUri.getScheme() == null) { + // this is probably an asset embedded by the packager in resources + InputStream in = openResourceInputStream((String) options.get("from")); + OutputStream out = new FileOutputStream(uriToFile(toUri)); + IOUtils.copy(in, out); + promise.resolve(null); } else { throw new IOException("Unsupported scheme for location '" + fromUri + "'."); } From 988d0da4f611702e378664280f2730a770ac365b Mon Sep 17 00:00:00 2001 From: Eric Samelson Date: Mon, 4 May 2020 14:15:09 -0700 Subject: [PATCH 2/2] add CHANGELOG entry --- packages/expo-file-system/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/expo-file-system/CHANGELOG.md b/packages/expo-file-system/CHANGELOG.md index bf03e9c6bb195..41e81a24007f2 100644 --- a/packages/expo-file-system/CHANGELOG.md +++ b/packages/expo-file-system/CHANGELOG.md @@ -11,5 +11,6 @@ ### 🎉 New features - Add `FileSystem.uploadAsync` method. ([#7380](https://github.com/expo/expo/pull/7380) by [@lukmccall](https://github.com/lukmccall)) +- Add ability to read Android `raw` and `drawable` resources in `FileSystem.getInfoAsync`, `FileSystem.readAsStringAsync`, and `FileSystem.copyAsync`. ([#8104](https://github.com/expo/expo/pull/8104) by [@esamelson](https://github.com/esamelson)) ### 🐛 Bug fixes