-1

我想将文件从内部存储移动到 SD 卡。我试过了,Environment.getExternalStorageDirectory()但它只移动内部存储。

我使用了以下代码:

ContextCompat.getExternalFilesDirs(mActivity, null)[0]

但它正在包文件夹中移动

/storage/emulated/0/Android/data/com.unzipdemo/files/MyFileStorage/SampleFile.txt

我想移动特定文件夹名称中的文件。你能帮我解决它吗?

4

1 回答 1

0

试试这个方法copyDirectoryOneLocationToAnotherLocation()

将内部文件值作为源位置传递,将外部文件路径作为目标位置传递

public static void copyDirectoryOneLocationToAnotherLocation(File sourceLocation, File
            targetLocation)
            throws IOException {

        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists()) {
                targetLocation.mkdir();
            }

            String[] children = sourceLocation.list();
            for (int i = 0; i < sourceLocation.listFiles().length; i++) {

                copyDirectoryOneLocationToAnotherLocation(new File(sourceLocation, children[i]),
                        new File(targetLocation, children[i]));
            }
        } else {

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

    }

注意:- 复制后删除源文件

于 2019-09-09T09:31:31.450 回答