【问题标题】:Move file from internal storage to Sd card将文件从内部存储移动到 SD 卡
【发布时间】:2020-01-11 00:09:52
【问题描述】:

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

我用过以下代码:

ContextCompat.getExternalFilesDirs(mActivity, null)[0]

但它正在包文件夹中移动

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

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

【问题讨论】:

    标签: android sd-card movefile


    【解决方案1】:

    试试这个方法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();
            }
    
        }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      • 2017-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多