Skip to content

Commit

Permalink
Fixed file moving
Browse files Browse the repository at this point in the history
  • Loading branch information
adamburnett committed Jul 13, 2018
1 parent fb0c5f1 commit 5837361
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 5837361

Please sign in to comment.