From d6b2f78ad45b1ca3a8788fb92bb04fb98dc90d80 Mon Sep 17 00:00:00 2001 From: Alston Lin Date: Mon, 18 Mar 2019 12:56:12 -0700 Subject: [PATCH] Added support for saving http and https URIs in the React Native CameraRoll module Summary: Currently, the behavior of saving in the `CameraRoll` module varies depending on the platform. On iOS, remote URIs are supported and will save a remote image to the local storage. However, on Android it can only be used for local images, making this function much less useful. This change allows the Android version of the `CameraRoll` module to have the same behavior, at least for http and https scheme URIs, which will probably cover the vast majority of the use cases. Reviewed By: sahrens Differential Revision: D14483943 fbshipit-source-id: e63ca786240e6657c8c8e0292be9fb08efa40ef1 --- .../react/modules/camera/CameraRollManager.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/camera/CameraRollManager.java b/ReactAndroid/src/main/java/com/facebook/react/modules/camera/CameraRollManager.java index b28351c1d00..ceb838df72b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/camera/CameraRollManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/camera/CameraRollManager.java @@ -43,11 +43,14 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.channels.Channels; import java.nio.channels.FileChannel; +import java.nio.channels.ReadableByteChannel; import java.util.ArrayList; import java.util.List; import javax.annotation.Nullable; import java.net.URLConnection; +import java.net.URL; // TODO #6015104: rename to something less iOSish /** @@ -97,7 +100,7 @@ public class CameraRollManager extends ReactContextBaseJavaModule { * from wherever it may be to the external storage pictures directory, so that it can be scanned * by the MediaScanner. * - * @param uri the file:// URI of the image to save + * @param uri the file://, http:// or https:// URI of the image to save * @param promise to be resolved or rejected */ @ReactMethod @@ -121,9 +124,16 @@ public class CameraRollManager extends ReactContextBaseJavaModule { @Override protected void doInBackgroundGuarded(Void... params) { + ReadableByteChannel input = null; + FileChannel output = null; File source = new File(mUri.getPath()); - FileChannel input = null, output = null; try { + String scheme = mUri.getScheme(); + if (scheme.equals("http") || scheme.equals("https")){ + input = Channels.newChannel(new URL(mUri.toString()).openStream()); + } else { + input = new FileInputStream(source).getChannel(); + } File exportDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); exportDir.mkdirs(); if (!exportDir.isDirectory()) { @@ -144,9 +154,8 @@ public class CameraRollManager extends ReactContextBaseJavaModule { while (!dest.createNewFile()) { dest = new File(exportDir, sourceName + "_" + (n++) + sourceExt); } - input = new FileInputStream(source).getChannel(); output = new FileOutputStream(dest).getChannel(); - output.transferFrom(input, 0, input.size()); + output.transferFrom(input, 0, Long.MAX_VALUE); input.close(); output.close();