Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically move files from internal memory to sdcard in android?

Tags:

file

android

move

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();
    }

}
like image 885
user5054103 Avatar asked Nov 10 '22 10:11

user5054103


1 Answers

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.

like image 82
George Daramouskas Avatar answered Nov 14 '22 21:11

George Daramouskas