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

前端 未结 5 1037
北荒
北荒 2020-12-08 08:50

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


        
5条回答
  •  时光取名叫无心
    2020-12-08 08:56

    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);
        }
    

提交回复
热议问题