From 5837361a5dd0b5997380bf7a3240e13b567dee69 Mon Sep 17 00:00:00 2001 From: adamburnett Date: Thu, 12 Jul 2018 18:05:03 -0600 Subject: [PATCH] Fixed file moving --- .../java/com/RNFetchBlob/RNFetchBlobFS.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java b/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java index 5d4d8edcd..6eba4f15a 100644 --- a/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java +++ b/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java @@ -599,16 +599,36 @@ static void mv(String path, String dest, Callback callback) { callback.invoke("Source file at path `" + path + "` does not exist"); return; } + + //Check if the output file directory exists. + File dir = new File(dest); + if (!dir.exists()) + { + dir.mkdirs(); + } + try { - boolean result = src.renameTo(new File(dest)); - if (!result) { - callback.invoke("mv failed for unknown reasons"); - return; + InputStream in = new FileInputStream(path); + OutputStream out = new FileOutputStream(dest); + + //read source path to byte buffer. Write from input to output stream + byte[] buffer = new byte[1024]; + int read; + while ((read = in.read(buffer)) != -1) { //read is successful + out.write(buffer, 0, read); } + in.close(); + out.flush(); + + src.delete(); //remove original file + } catch (FileNotFoundException exception) { + callback.invoke(exception.toString()); + return; } catch (Exception e) { - callback.invoke(e.getLocalizedMessage()); + callback.invoke(e.toString()); return; } + callback.invoke(); }