Copy file from the directory to another , having t

2019-06-01 18:18发布

in my android application I want to copy file from one directory to another , I have the path of file filePath , and have the path of directory dirPath in what I must copy the file. I tried many ways , but nothing helped , some ways only make some empty(0 kb) file with strange name different from the name of my file. SO help please :)

here is some part of code, if it help's you, I have two buttons for Gallery and for Camera , and I must pick images from there

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

and Activity Result

@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
      }  
  }

2条回答
一纸荒年 Trace。
2楼-- · 2019-06-01 18:49

If you feel up to it, you can include Apache Commons I/O in your app. I don't know how big it is. See Standard concise way to copy a file in Java?

Or maybe you could download the source, and cherry-pick the source to FileUtils.copyFile()

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-06-01 19:02

I found some way , in my Activity result I must call copyFile(String, String) function , here is the body

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;
        }
    } 
查看更多
登录 后发表回答