ソースを参照

Merge pull request #114 from ihavenoface5/FileMoving

Fixed file moving
Travis Nuttall 5 年 前
コミット
41e1572315
No account linked to committer's email address
共有1 個のファイルを変更した25 個の追加5 個の削除を含む
  1. 25
    5
      android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

+ 25
- 5
android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java ファイルの表示

@@ -599,16 +599,36 @@ class RNFetchBlobFS {
599 599
             callback.invoke("Source file at path `" + path + "` does not exist");
600 600
             return;
601 601
         }
602
+
603
+        //Check if the output file directory exists.
604
+        File dir = new File(dest);
605
+        if (!dir.exists())
606
+        {
607
+            dir.mkdirs();
608
+        }
609
+
602 610
         try {
603
-            boolean result = src.renameTo(new File(dest));
604
-            if (!result) {
605
-                callback.invoke("mv failed for unknown reasons");
606
-                return;
611
+            InputStream in = new FileInputStream(path);
612
+            OutputStream out = new FileOutputStream(dest);
613
+
614
+            //read source path to byte buffer. Write from input to output stream
615
+            byte[] buffer = new byte[1024];
616
+            int read;
617
+            while ((read = in.read(buffer)) != -1) { //read is successful
618
+                out.write(buffer, 0, read);
607 619
             }
620
+            in.close();
621
+            out.flush();
622
+
623
+            src.delete(); //remove original file
624
+        } catch (FileNotFoundException exception) {
625
+            callback.invoke(exception.toString());
626
+            return;
608 627
         } catch (Exception e) {
609
-            callback.invoke(e.getLocalizedMessage());
628
+            callback.invoke(e.toString());
610 629
             return;
611 630
         }
631
+
612 632
         callback.invoke();
613 633
     }
614 634