【问题标题】:Copy file from the directory to another , having the path's of file and directory将文件从目录复制到另一个,具有文件和目录的路径
【发布时间】:2012-09-19 14:19:43
【问题描述】:

在我的 android 应用程序中,我想将文件从一个目录复制到另一个目录,我有文件 filePath 的路径,并且在我必须复制文件的地方有目录 dirPath 的路径。我尝试了很多方法,但没有任何帮助,有些方法只会制作一些与我的文件名不同的奇怪名称的空(0 kb)文件。所以请帮助:)

这是代码的一部分,如果对你有帮助,我有两个按钮用于 Gallery 和 Camera ,我必须从那里挑选图像

Button btnCam = (Button) dialog.findViewById(R.id.btncamera);
                btnCam.setOnClickListener(new View.OnClickListener() 
                {
                    @Override
                    public void onClick(View v) 
                    {
                        dialog.cancel();
                        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(cameraIntent, 2500);

                    }
                });
                //end of camera button 


                Button btnGal = (Button) dialog.findViewById(R.id.btngalary);
                btnGal.setOnClickListener(new OnClickListener() 
                {
                    @Override
                    public void onClick(View v) 
                    {
                        dialog.cancel();
                        Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(i, RESULT_LOAD_IMAGE);
                    }
                });

和活动结果

@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) 
  {
      super.onActivityResult(requestCode, resultCode, data);

      if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) 
      {
          Uri selectedImage = data.getData();
          url = new String(selectedImage.toString()); 
          //here I must copy file with path `selectedImage` and replace it in 
          // directory with path `currentDir.hetPath()` 
      }
      if (requestCode == 2500) 
      {  
          Bitmap photo = (Bitmap) data.getExtras().get("data"); 
          imageView.setImageBitmap(photo);
          //here I must do the same thing above
      }  
  }

【问题讨论】:

  • 如果您不向我们展示您的代码,我们将无济于事 :)
  • :) 我的代码很大,但我会添加一些代码 :) ,我只需要一些可以给他 filePath 和 dirPath 的函数,它会将文件复制到目录:)

标签: java android file copy copy-paste


【解决方案1】:

我找到了一些方法,在我的 Activity 结果中我必须调用copyFile(String, String) 函数,这是正文

public static boolean copyFile(String from, String to) {
        try {
            File sd = Environment.getExternalStorageDirectory();
            if (sd.canWrite()) {
                int end = from.toString().lastIndexOf("/");
                String str1 = from.toString().substring(0, end);
                String str2 = from.toString().substring(end+1, from.length());
                File source = new File(str1, str2);
                File destination= new File(to, str2);
                if (source.exists()) {
                    FileChannel src = new FileInputStream(source).getChannel();
                    FileChannel dst = new FileOutputStream(destination).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                }
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    } 

【讨论】:

  • 这是最好的答案恕我直言。另外:我希望不用说您应该在单独的线程中执行此操作。
  • 如果从主线程复制,整个界面会冻结,直到复制完成。如果这需要超过大约 1/4 秒,用户会发现体验不愉快。如果需要更长的时间,应用程序可能会因“应用程序无响应”(ANR) 而崩溃。因此,如果可能的话,您应该启动一个单独的线程(可能是 AsyncTask),以便复制可以在后台运行,而不会对用户体验造成太大影响。
【解决方案2】:

如果您愿意,可以在您的应用中包含 Apache Commons I/O。我不知道它有多大。见Standard concise way to copy a file in Java?

或者也许您可以下载源代码,然后将源代码挑选到 FileUtils.copyFile()

【讨论】:

    猜你喜欢
    • 2013-09-30
    • 2017-11-18
    • 2012-02-15
    • 2013-06-01
    • 2020-06-22
    • 2014-08-12
    • 2013-10-24
    • 2011-10-24
    相关资源
    最近更新 更多