From 9498dbfd93eadf25052ed59888d442d6e21cc130 Mon Sep 17 00:00:00 2001 From: Kyle Corbitt Date: Thu, 4 Aug 2016 17:54:01 -0700 Subject: [PATCH] move-file fallback --- .../src/main/java/com/rnfs/RNFSManager.java | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/android/src/main/java/com/rnfs/RNFSManager.java b/android/src/main/java/com/rnfs/RNFSManager.java index d65f883..2b84e10 100644 --- a/android/src/main/java/com/rnfs/RNFSManager.java +++ b/android/src/main/java/com/rnfs/RNFSManager.java @@ -1,5 +1,6 @@ package com.rnfs; +import java.io.IOException; import java.util.Map; import java.util.HashMap; @@ -125,9 +126,13 @@ public class RNFSManager extends ReactContextBaseJavaModule { @ReactMethod public void moveFile(String filepath, String destPath, Promise promise) { try { - File from = new File(filepath); - File to = new File(destPath); - from.renameTo(to); + File inFile = new File(filepath); + + if (!inFile.renameTo(new File(destPath))) { + copyFile(filepath, destPath); + + inFile.delete(); + } promise.resolve(true); } catch (Exception ex) { @@ -139,16 +144,7 @@ public class RNFSManager extends ReactContextBaseJavaModule { @ReactMethod public void copyFile(String filepath, String destPath, Promise promise) { try { - InputStream in = new FileInputStream(filepath); - OutputStream out = new FileOutputStream(destPath); - - byte[] buffer = new byte[1024]; - int length; - while ((length = in.read(buffer)) > 0) { - out.write(buffer, 0, length); - } - in.close(); - out.close(); + copyFile(filepath, destPath); promise.resolve(null); } catch (Exception ex) { @@ -157,6 +153,19 @@ public class RNFSManager extends ReactContextBaseJavaModule { } } + private void copyFile(String filepath, String destPath) throws IOException { + InputStream in = new FileInputStream(filepath); + OutputStream out = new FileOutputStream(destPath); + + byte[] buffer = new byte[1024]; + int length; + while ((length = in.read(buffer)) > 0) { + out.write(buffer, 0, length); + } + in.close(); + out.close(); + } + @ReactMethod public void readDir(String directory, Promise promise) { try {