mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-03-06 22:37:14 +08:00
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
This commit is contained in:
committed by
Facebook Github Bot
parent
75af15ede4
commit
d6b2f78ad4
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user