Procházet zdrojové kódy

Merge pull request #114 from ihavenoface5/FileMoving

Fixed file moving
Travis Nuttall před 5 roky
rodič
revize
41e1572315
No account linked to committer's email address
1 změnil soubory, kde provedl 25 přidání a 5 odebrání
  1. 25
    5
      android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

+ 25
- 5
android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java Zobrazit soubor

599
             callback.invoke("Source file at path `" + path + "` does not exist");
599
             callback.invoke("Source file at path `" + path + "` does not exist");
600
             return;
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
         try {
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
         } catch (Exception e) {
627
         } catch (Exception e) {
609
-            callback.invoke(e.getLocalizedMessage());
628
+            callback.invoke(e.toString());
610
             return;
629
             return;
611
         }
630
         }
631
+
612
         callback.invoke();
632
         callback.invoke();
613
     }
633
     }
614
 
634