Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中将文件(img/doc)从特定路径复制到另一个路径_Android_File Io - Fatal编程技术网

在Android中将文件(img/doc)从特定路径复制到另一个路径

在Android中将文件(img/doc)从特定路径复制到另一个路径,android,file-io,Android,File Io,我的应用程序中有一个功能,我在数据库中保存一个doc/img文件路径。此文件位于文件夹中(例如“/mnt/sdcard/MyApp/MyItem/test.png”)。现在我想做的是将这个文件复制到其他文件夹(例如/mnt/sdcard/MyApp/MyItem/Today/test.png) 现在我正在使用下面的代码,但它不起作用: private void copyDirectory(File from, File to) throws IOException { try {

我的应用程序中有一个功能,我在数据库中保存一个doc/img文件路径。此文件位于文件夹中(例如“/mnt/sdcard/MyApp/MyItem/test.png”)。现在我想做的是将这个文件复制到其他文件夹(例如/mnt/sdcard/MyApp/MyItem/Today/test.png)

现在我正在使用下面的代码,但它不起作用:

private void copyDirectory(File from, File to) throws IOException {


    try {
        int bytesum = 0;
        int byteread = 0;

            InputStream inStream = new FileInputStream(from);
            FileOutputStream fs = new FileOutputStream(to);
            byte[] buffer = new byte[1444];
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                fs.write(buffer, 0, byteread);
            }
            inStream.close();
            fs.close();

    } catch (Exception e) {
    }
}
在按钮上单击am,使用以下代码:

File sourceFile=新文件(fileList.get(0.getAbsolutePath)//来自星展银行
File targetFile=新文件(Environment.getExternalStorageDirectory(),“MyApp/MyItem/Today/”;
copyDirectory(sourceFile、targetFile、currDateStr)


知道它为什么不工作吗?

这段代码对我来说很好用

public void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}
另外,您在清单文件中添加了一项写入外部存储的权限*


是的,我在复制文件时没有给出文件名,也没有真正查看错误日志,现在可以工作了,谢谢。是的,上面的代码工作得很好。

尝试调用fs.flush();
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />