Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 499
Prashant Kadam Avatar asked Dec 29 '11 05:12

Prashant Kadam


3 Answers

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);
    }
like image 58
Prashant Kadam Avatar answered Nov 15 '22 18:11

Prashant Kadam


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

like image 27
Richa Avatar answered Nov 15 '22 18:11

Richa


one solution can be,

1) read bytes from inputStream of the picked file.

i get "content://media/external/images/media/681" this URI onActivityResult. You can get the file name by querying this Uri u got. get inputStream of it. read it into byte[].

here you go/

Uri u = Uri.Parse("content://media/external/images/media/681");

Cursor cursor = contentResolver.query(u, null, null, null, null); there is a column name "_data" which will return you the filename, from filename you can create inputstream,

you can now read this input stream

         byte data=new byte[fis.available()];
          fis.read(data);

So you have data(byte array) with images byte

2) create a file on to sdcard, and write with byte[] taken in step one.

       File file=new File(fileOnSD.getAbsolutePath() +"your foldername", fileName);
        FileOutputStream fout=new FileOutputStream(file, false);
        fout.write(data);

as fileName you already have from the query method, use same here.

like image 5
AAnkit Avatar answered Nov 15 '22 20:11

AAnkit