2

With all of the changes in android 10 that are related to scoped storage, how can we open the camera. I saw some tutorial that suggest using a file provider, but I don't really quite get it. Can you provide a snippet of code to launch the camera intent, and receive the taken image ?

Edit : In this article : https://medium.com/@arkapp/accessing-images-on-android-10-scoped-storage-bbe65160c3f4 He provided this code :

    fun takePicture(context: Activity, imageName: String) {try {
  val capturedImgFile = File(
      context.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
      imageName)

  captureImgUri = FileProvider.getUriForFile(
      context, 
      context.applicationContext.packageName + ".my.package.name.provider",
      capturedImgFile)val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE).also {

   it.putExtra(MediaStore.EXTRA_OUTPUT, captureImgUri)
   it.putExtra("return-data", true)
   it.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
   context.startActivityForResult(it, REQUEST_CODE_TAKE_PICTURE)

  }
 } catch (e: ActivityNotFoundException) {e.printStackTrace()}
}@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

 if (resultCode != RESULT_OK) {return}

 if (requestCode == REQUEST_CODE_TAKE_PICTURE) {

  /*We cannot access the image directly so we again create a new File at different location and use it for futher processing.*/

  Bitmap capturedBitmap = getBitmap(this, captureImgUri)

  /*We are storing the above bitmap at different location where we can access it.*/  val capturedImgFile = File(
   getExternalFilesDir(Environment.DIRECTORY_PICTURES), 
   getTimestamp() + "_capturedImg.jpg");  convertBitmaptoFile(capturedImgFile, capturedBitmap)/*We have to again create a new file where we will save the processed image.*/  val croppedImgFile = File(
    getExternalFilesDir(Environment.DIRECTORY_PICTURES), 
    getTimestamp() + "_croppedImg.jpg");  startCrop(
     this,
     Uri.fromFile(capturedImgFile),
     Uri.fromFile(croppedImgFile))}
 super.onActivityResult(requestCode, resultCode, data)
}

I have two questions:

  • What does this line do : it.putExtra("return-data", true)
  • in OnActivityResulty, why didn't he just use the uri directly, he first created a file and then parsed the file into an uri, what is this for ? how does it have to do android 10 scoped storage ?
ahmed osama
  • 560
  • 1
  • 7
  • 13

1 Answers1

3

What does this line do : it.putExtra("return-data", true)

Nothing, usually. It's possible that there are some camera apps that will do something when receiving this undocumented and (generally) unsupported Intent extra.

in OnActivityResulty, why didn't he just use the uri directly

He does, in earlier steps. By the end, he wants to use uCrop to allow the user to crop the image. Perhaps he wants to hold onto both the original and the cropped photo.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • so, what did he mean by "//We cannot access the image directly so we again create a new File at different location and use it for futher processing" – ahmed osama Apr 23 '20 at 21:19
  • @ahmedosama: I have no idea. Frankly, I do not think that it is a very good Medium post. If you want a sample app, [this one of mine](https://github.com/commonsguy/cw-omnibus/tree/v9.0/Camera/FileProvider) is a bit old but may help. – CommonsWare Apr 23 '20 at 21:28