1

In my android application i want to copy the media images to another folder(in my below code i try to copy picture from "/mnt/sdcard/DCIM/Camera/my_photo.jpg" to "/mnt/sdcard/PortFolio/MyGallery/ . I tried this with the following code but it doesn't work. Someone help me out of this??? Is there any other way possible??

File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
            if (sd.canWrite()) {
                String sourceImagePath= "/mnt/sdcard/DCIM/Camera/my_photo.jpg";
                String destinationImagePath= "/mnt/sdcard/PortFolio/MyGallery/";
                Log.d("destinationImagePath", ""+destinationImagePath);
                File source= new File(data, sourceImagePath);
                File destination= new File(sd, destinationImagePath);
                Log.d("before copying", "");
                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();
                }
kichik
  • 33,220
  • 7
  • 94
  • 114
Santhosh_pulliman
  • 2,119
  • 6
  • 30
  • 47

1 Answers1

1

sd already contains /mnt/sdcard. You are actually trying to open /mnt/sdcard/mnt/sdcard/DCIM/Camera/my_photo.jpg. Remove /mnt/sdcard from sourceImagePath and destinationImagePath. You would probably also need to create the PortFolio/MyGallery folder first.

Starting API level 8, you can also use this to get the default pictures folder:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

And last but not least, make sure you have permission to access the SD card.

Community
  • 1
  • 1
kichik
  • 33,220
  • 7
  • 94
  • 114