Copy or Move File From One Directory to Another on Android

Photo of Mike Dalisay
Modified Wednesday, April 10, 2013
by - @ninjazhai
Today we are going to take a look at how to programmatically "copy" or "move" a file from one directory to another on Android. In case you want to be clarified about the difference, copying a file means the file will be seen on another directory (target location) without deleting the original file from the source location. Moving a file means the file will be seen in another directory and the original file will be deleted from its former location.

Logcat output when we successfuly copied a file. Click to enlarge.

Here are some situations where I found these functionalities helpful:

  • Copying a file to another directory is useful when you have to save a file's version or state in specific time.
  • Moving a file to another directory can be an advantage when you want to remove a file from a main folder of your application and make it an archive.

android copy or move a file

Before running this code, you have to:
  • Place an example text file named "sample.txt" (with any text inside) in your SD card root.
  • Create a directory called "MyNewFolder" in your SD card root directory also.

package com.example.copyfilefromdirectorytoanother;


import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class MainActivity extends Activity {

    private static final String TAG = "";
    protected void onCreate(Bundle savedInstanceState) {

        // your sd card
        String sdCard = Environment.getExternalStorageDirectory().toString();
        // the file to be moved or copied
        File sourceLocation = new File (sdCard + "/sample.txt");
        // make sure your target location folder exists!
        File targetLocation = new File (sdCard + "/MyNewFolder/sample.txt");

        // just to take note of the location sources
        Log.v(TAG, "sourceLocation: " + sourceLocation);
        Log.v(TAG, "targetLocation: " + targetLocation);
        try {
            // 1 = move the file, 2 = copy the file
            int actionChoice = 2;
            // moving the file to another directory
                    Log.v(TAG, "Move file successful.");
                    Log.v(TAG, "Move file failed.");
            // we will copy the file
                // make sure the target file exists
                    InputStream in = new FileInputStream(sourceLocation);
                    OutputStream out = new FileOutputStream(targetLocation);
                    // Copy the bits from instream to outstream
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = > 0) {
                        out.write(buf, 0, len);
                    Log.v(TAG, "Copy file successful.");
                    Log.v(TAG, "Copy file failed. Source file missing.");
        } catch (NullPointerException e) {
        } catch (Exception e) {

Logcat ouput when we move a file looks like this:

Click to enlarge.

Logcat output when copying a file failed:

Click to enlarge.

Thanks for reading this quick post! :)
For FREE programming tutorials, click the red button below and subscribe! :)
Thanks for the comments!
"First do it, then do it right, then do it better."
~ Addy Osmani
"Talk is cheap. Show me the code."
~ Linus Torvalds
Let's Stay Connected!
g+ r
Android app on Google Play
© 2011-2014 The Code Of A Ninja. All rights reserved. Proudly Powered by Google Blogger. Images, logos, marks or names mentioned herein are the property of their respective owners.