How to move files from device's internal memory to external memory in android? Please provide code examples. My code is below
private void moveFile(File file, File dir) throws IOException {
File newFile = new File(dir, file.getName());
FileChannel outputChannel = null;
FileChannel inputChannel = null;
try {
outputChannel = new FileOutputStream(newFile).getChannel();
inputChannel = new FileInputStream(file).getChannel();
inputChannel.transferTo(0, inputChannel.size(), outputChannel);
inputChannel.close();
file.delete();
} finally {
if (inputChannel != null) inputChannel.close();
if (outputChannel != null) outputChannel.close();
}
}
Remove the trailing slash from your path. It is not needed since example.png is not a directory. Actually do not hardcode the path to the sd card cause it might differ from device to device. Try Environment.getExternalStorageDirectory() to get the path to sdcard then add any trailing path in the end.
Please take a look at the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With