将文件从目录复制到另一个目录,具有文件和目录的路径

Hay*_*yan 1 java android copy copy-paste file

在我的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);
                    }
                });
Run Code Online (Sandbox Code Playgroud)

和活动结果

@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
      }  
  }
Run Code Online (Sandbox Code Playgroud)

Hay*_*yan 10

我找到了一些方法,在我的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;
        }
    } 
Run Code Online (Sandbox Code Playgroud)

  • 如果从主线程执行复制,整个界面将冻结,直到复制完成.如果这花费超过大约1/4秒,则用户将发现体验令人不愉快.如果需要更长时间,应用程序可能会因"应用程序无响应"(ANR)而崩溃.因此,如果可能的话,您应该启动一个单独的线程(可能是AsyncTask),以便复制可以在后台运行,而不会太严重地影响用户体验. (3认同)