codeofaninja
website

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.
Our MainActivity.java

package com.example.copyfilefromdirectorytoanother;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

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

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity.java";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 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
            if(actionChoice==1){
                
                if(sourceLocation.renameTo(targetLocation)){
                    Log.v(TAG, "Move file successful.");
                }else{
                    Log.v(TAG, "Move file failed.");
                }
                
            }
            
            // we will copy the file
            else{
                
                // make sure the target file exists
                
                if(sourceLocation.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 = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    
                    in.close();
                    out.close();
                    
                    Log.v(TAG, "Copy file successful.");
                    
                }else{
                    Log.v(TAG, "Copy file failed. Source file missing.");
                }
                
            }
            
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


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!
 
 
Fundamentals
"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.