Android:将文件从一个目录复制到另一个目录

时间:2013-10-16 16:47:33

标签: android file directory copy

我正在使用以下代码并使用apache commons库将一个文件(临时音频文件)从源文件夹复制到目标文件夹TT_1A。

代码:

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

问题:

我使用了FileUtils函数来复制临时文件并粘贴到desc文件夹中作为TT_1A。然而,logcat报告错误,其中包含以下详细信息:

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)

整个晚上我一直坚持这一点。有没有办法复制文件??

3 个答案:

答案 0 :(得分:22)

经过以下方式修改后运行良好:

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

答案 1 :(得分:1)

在API级别29中,android添加了具有复制功能的 FileUtils 类。

使用此代码复制文件:-

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

如果Android Studio找不到 FileUtils 类,则意味着您的compileSdkVersion低于29,在这种情况下,您必须升级compileSdkVersion。

要升级,请转到您的应用程序级别的build.gradle文件,然后将旧的 compileSdkVersion 替换为29

compileSdkVersion 29

有关 FileUtils 类的更多信息,请访问Android开发者网站 https://developer.android.com/reference/android/os/FileUtils

答案 2 :(得分:0)

使用 srcFile.copyTo(destinationFile)