Select Image From Gallery

Select Image From Gallery

1. Select Single Image from Gallery / Storage

  • To take image from gallery into android app we can use already available library
  1. Include the library
dependencies { api 'com.theartofdev.edmodo:android-image-cropper:2.8.+' }
Add permissions to manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Using Activity

  1. Add CropImageActivity into your AndroidManifest.xml
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity" android:theme="@style/Base.Theme.AppCompat"/> <!-- optional (needed if default theme has no action bar) -->
  1. Start CropImageActivity using builder pattern from your activity
// start picker to get image for cropping and then use the image in cropping activity CropImage.activity() .setGuidelines(CropImageView.Guidelines.ON) .start(this); // start cropping activity for pre-acquired image saved on the device CropImage.activity(imageUri) .start(this); // for fragment CropImage.activity() .start(getContext(), this);
  1. Override onActivityResult method in your activity to get crop result
      • Deperecated in java
@Deprecated("Deprecated in Java") override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) { val result = CropImage.getActivityResult(data) if (resultCode == AppCompatActivity.RESULT_OK) { val resultUri = result.uri // set action to perform on image } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) { val error = result.error } } }
 
 

1. Select Image from gallery using intent

  1. Using deprecated method
binding.image.setOnClickListener { val intent = Intent(Intent.ACTION_GET_CONTENT) intent.type = "image//*" only use one slash startActivityForResult(Intent.createChooser(intent, "Select Picture"), 100) } // on class level @Deprecated("Deprecated in Java") override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (resultCode == RESULT_OK) { if (requestCode == 100) { val selectedImageUri = data!!.data binding.image.setImageURI(selectedImageUri) } } }
  1. Using newly used method
      • in this mrthod we dount have to worry about request code
binding.image.setOnClickListener { val intent = Intent(Intent.ACTION_GET_CONTENT) intent.type = "image//*" only use one slas imageLauncher.launch(intent) } // on class level private val imageLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { if (it.resultCode == RESULT_OK) { if (it.data != null) { val selectedImageUri = it.data!!.data binding.image.setImageURI(selectedImageUri) } } }
 
 

2. Select Multiple Images from gallery

// define numbers of images to be selected private val REQUEST_CODE= 5 // put this on some button val intent = Intent(Intent.ACTION_GET_CONTENT) intent.setType("image/*") */ // define type to be selected // Important Line to select images intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true) startActivityForResult( Intent.createChooser(intent, "Select Picture"), REQUEST_CODE )
 
  • Selected File will be get in onActivityResult function
  • Now if our REQUEST_CODE match than we can process on data
@Deprecated("Deprecated in Java") override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == REQUEST_CODE) { if (resultCode == RESULT_OK) { if (data?.clipData != null) { val count = data.clipData!!.itemCount //evaluate the count before the for loop --- otherwise, the count is evaluated every loop. imageUriArray.clear() imageStringArray.clear() for (i in 0 until count) { val imageUri = data.clipData?.getItemAt(i).uri //do something with the image //(save it to some directory or whatever you need to do with it here) } } } else if (data?.data != null) { val imagePath = data.data?.path //do something with the image (save it to some directory or whatever you need to do with it here) } } }