【问题标题】:How to copy a file to another directory programmatically?如何以编程方式将文件复制到另一个目录?
【发布时间】:2015-04-25 15:28:08
【问题描述】:

directory 中有一个image file。如何将copy 这个image file 变成另一个刚刚创建的directory?两个directories 在设备的同一个internal storage 上:)

【问题讨论】:

  • 带代码还是一般?
  • 以编程方式,带代码:)
  • 我给了你任何递归函数。它允许复制单个文件或整个目录及其所有子目录。干杯。

标签: android


【解决方案1】:

您可以使用这些功能。如果您传入一个文件,第一个将复制整个目录以及所有子目录或单个文件。第二个只对文件有用,并为第一个中的每个文件调用。

另请注意,您需要拥有执行此操作的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

功能:

 public static void copyFileOrDirectory(String srcDir, String dstDir) {

        try {
            File src = new File(srcDir);
            File dst = new File(dstDir, src.getName());

            if (src.isDirectory()) {

                String files[] = src.list();
                int filesLength = files.length;
                for (int i = 0; i < filesLength; i++) {
                    String src1 = (new File(src, files[i]).getPath());
                    String dst1 = dst.getPath();
                    copyFileOrDirectory(src1, dst1);

                }
            } else {
                copyFile(src, dst);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void copyFile(File sourceFile, File destFile) throws IOException {
        if (!destFile.getParentFile().exists())
            destFile.getParentFile().mkdirs();

        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        FileChannel source = null;
        FileChannel destination = null;

        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }

【讨论】:

  • 如何获得进度?
  • 你不能使用这个实现,因为你不知道前面有多少文件,也不知道它们的大小......如果你想要进度,你需要知道这一点,这最终会减慢这个下来。如果你想要表现,那就搞砸进度。
  • 你可以使用try (FileChannel source = new FileInputStream(sourceFile).getChannel(); FileChannel destination = new FileOutputStream(destFile).getChannel())来避免使用finally
  • 完美解决方案。为我完美解决。
【解决方案2】:

如果您想以编程方式复制图像,请使用以下代码。

     File sourceLocation= new File (sourcepath);
     File targetLocation= new File (targetpath);

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

【讨论】:

    【解决方案3】:

    ** 使用 FileUtils 这是简单快速且最佳的方法并从此处下载 Jar 文件**

    public void MoveFiles(String sourcepath) {

             File source_f = new File(sourcepath);
            
            String destinationPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/WhatsappStatus/yourfilename.mp4";
            File destination = new File(destinationPath);
            try
            {
                FileUtils.copyFile(source_f , destination);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        } 
    

    Go To Link For FileUtils Jar

    【讨论】:

      猜你喜欢
      • 2015-10-30
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      • 2017-11-18
      • 2012-03-24
      • 1970-01-01
      相关资源
      最近更新 更多