Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: copying files from one directory to another

I am working on copying one file (the temp audio file) from source folder to the destination folder as TT_1A using the following code with the use of apache commons library.

Code:

button1_save.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            String sourcePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TT/tt_temp.3gp";
            File source = new File(sourcePath);

            String descPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TT/tt_1A.3gp";
            File desc = new File(descPath);
            try 
            {
                FileUtils.copyDirectory(source, desc);
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
    }); 

Apache Commons:

http://commons.apache.org/proper/commons-io/download_io.cgi

Question:

I have used the FileUtils function so as to copy the temp file and paste to the desc folder as TT_1A. Yet logcat reports error with the following details:

10-17 00:41:04.260: E/AndroidRuntime(27450): FATAL EXCEPTION: main
10-17 00:41:04.260: E/AndroidRuntime(27450): java.lang.NoClassDefFoundError: org.apache.commons.io.FileUtils
10-17 00:41:04.260: E/AndroidRuntime(27450):    at com.abc.abc.TT_details_canton$14.onClick(TT_details_canton.java:575)
10-17 00:41:04.260: E/AndroidRuntime(27450):    at android.view.View.performClick(View.java:4223)
10-17 00:41:04.260: E/AndroidRuntime(27450):    at android.view.View$PerformClick.run(View.java:17275)
10-17 00:41:04.260: E/AndroidRuntime(27450):    at android.os.Handler.handleCallback(Handler.java:615)
10-17 00:41:04.260: E/AndroidRuntime(27450):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-17 00:41:04.260: E/AndroidRuntime(27450):    at android.os.Looper.loop(Looper.java:137)
10-17 00:41:04.260: E/AndroidRuntime(27450):    at android.app.ActivityThread.main(ActivityThread.java:4898)
10-17 00:41:04.260: E/AndroidRuntime(27450):    at java.lang.reflect.Method.invokeNative(Native Method)
10-17 00:41:04.260: E/AndroidRuntime(27450):    at java.lang.reflect.Method.invoke(Method.java:511)
10-17 00:41:04.260: E/AndroidRuntime(27450):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
10-17 00:41:04.260: E/AndroidRuntime(27450):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
10-17 00:41:04.260: E/AndroidRuntime(27450):    at dalvik.system.NativeStart.main(Native Method)

I have stucked by this for the whole night. Are there ways to copy files??

like image 923
pearmak Avatar asked Oct 16 '13 16:10

pearmak


People also ask

How do you copy the contents of a directory to another?

Answer: Use the cp Command You can use the cp command to copy files locally from one directory to another. The -a option copy files recursively, while preserving the file attributes such as timestamp. The period symbol ( . ) at end of the source path allows to copy all files and folders, including hidden ones.


2 Answers

It runs good after amended in the following way:

    button1_save.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            String sourcePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TongueTwister/tt_temp.3gp";
            File source = new File(sourcePath);

            String destinationPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TongueTwister/tt_1A.3gp";
            File destination = new File(destinationPath);
            try 
            {
                FileUtils.copyFile(source, destination);
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
    }); 
like image 59
pearmak Avatar answered Oct 14 '22 20:10

pearmak


In API level 29, android added FileUtils class, which has copy function.

Use this code to copy your file:-

public void copyFile(File source, File destination) throws IOException {
    FileUtils.copy(new FileInputStream(source), new FileOutputStream(destination));
}

If Android Studio does not find FileUtils class, that means your compileSdkVersion is lower than 29, in this case, you have to upgrade your compileSdkVersion.

For upgrading, go to your app-level build.gradle file and replace the old compileSdkVersion with 29

Like this

compileSdkVersion 29

For more info about FileUtils class, visit Android Developer Website https://developer.android.com/reference/android/os/FileUtils

like image 30
Nilesh Rathore Avatar answered Oct 14 '22 20:10

Nilesh Rathore