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 ?