How to Copy Image File from Gallery to another folder programmatically in Android

Prashant Kadam picture Prashant Kadam · Dec 29, 2011 · Viewed 45.7k times · Source

I want to pick image from gallery and copy it in to other folder in SDCard.

Code to Pick Image from Gallery

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE_PICTURE_FROM_GALLARY);

I get content://media/external/images/media/681 this URI onActivityResult.

I want to copy the image,

Form path ="content://media/external/images/media/681

To path = "file:///mnt/sdcard/sharedresources/ this path of sdcard in Android.

How to do this?

Answer

Prashant Kadam picture Prashant Kadam · Dec 30, 2011

Thanks to all ... Working Code is Here..

     private OnClickListener photoAlbumListener = new OnClickListener(){
          @Override
          public void onClick(View arg0) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
            imagepath = Environment.getExternalStorageDirectory()+"/sharedresources/"+HelperFunctions.getDateTimeForFileName()+".png";
            uriImagePath = Uri.fromFile(new File(imagepath));
            photoPickerIntent.setType("image/*");
            photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT,uriImagePath);
            photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.PNG.name());
            photoPickerIntent.putExtra("return-data", true);
            startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE_PICTURE_FROM_GALLARY);

          }
      };






   protected void onActivityResult(int requestCode, int resultCode, Intent data) {


           if (resultCode == RESULT_OK) {
                switch(requestCode){


                 case 22:
                        Log.d("onActivityResult","uriImagePath Gallary :"+data.getData().toString());
                        Intent intentGallary = new Intent(mContext, ShareInfoActivity.class);
                        intentGallary.putExtra(IMAGE_DATA, uriImagePath);
                        intentGallary.putExtra(TYPE, "photo");
                        File f = new File(imagepath);
                        if (!f.exists())
                        {
                            try {
                                f.createNewFile();
                                copyFile(new File(getRealPathFromURI(data.getData())), f);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }

                        startActivity(intentGallary);
                        finish();
                 break;


                }
              }





   }

   private void copyFile(File sourceFile, File destFile) throws IOException {
            if (!sourceFile.exists()) {
                return;
            }

            FileChannel source = null;
                FileChannel destination = null;
                source = new FileInputStream(sourceFile).getChannel();
                destination = new FileOutputStream(destFile).getChannel();
                if (destination != null && source != null) {
                    destination.transferFrom(source, 0, source.size());
                }
                if (source != null) {
                    source.close();
                }
                if (destination != null) {
                    destination.close();
                }


    }

    private String getRealPathFromURI(Uri contentUri) {

       String[] proj = { MediaStore.Video.Media.DATA };
       Cursor cursor = managedQuery(contentUri, proj, null, null, null);
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
       cursor.moveToFirst();
       return cursor.getString(column_index);
    }