how to move files from internal memory to external sdcard in android?

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

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

  • Answer:

    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 https://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory().

user5054103 at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.