Search code examples
androidandroid-intentandroid-galleryandroid-contentproviderandroid-file

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


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?


Solution

  • OutputStream out;
                String root = Environment.getExternalStorageDirectory().getAbsolutePath()+"/";
                File createDir = new File(root+"Folder Name"+File.separator);
                if(!createDir.exists()) {
                    createDir.mkdir();
                }
                File file = new File(root + "Folder Name" + File.separator +"Name of File");
                file.createNewFile();
                out = new FileOutputStream(file);                       
    
            out.write(data);
            out.close();
    

    Hope it will help u