1

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
      }  
  }
Hayk Nahapetyan
  • 4,452
  • 8
  • 42
  • 61

2 Answers2

10

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;
        }
    } 
Pateta
  • 430
  • 4
  • 15
Hayk Nahapetyan
  • 4,452
  • 8
  • 42
  • 61
  • 1
    This is the best answer IMHO. Also: I hope it goes without saying that you should do this in a separate thread. – Edward Falk Sep 24 '12 at 20:19
  • 3
    If you do the copy from the main thread, the entire interface will freeze up until the copy is complete. If this takes more than about 1/4 second, the user will find the experience to be unpleasant. If it takes even longer, the application can crash with "Application Not Responding" (ANR). So if at all possible, you should launch a separate thread (possibly an AsyncTask) so that the copying can run in the background without affecting the user experience too badly. – Edward Falk Sep 24 '12 at 23:41
1

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

Community
  • 1
  • 1
Edward Falk
  • 9,991
  • 11
  • 77
  • 112