How To Get Image Uri By Intent Mediastore.action_image_capture In Android 10 And Above
FOR MODERATORS: I know there are already questions like this but all of those approaches endup giving bitmap through data.getExtra('data') which is actually just thumbnail. I want
Solution 1:
This approach worked for me
In Manifest
file
<application><providerandroid:name="androidx.core.content.FileProvider"android:authorities="com.example.android.provider"android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/file_paths"></meta-data></provider>
...
</application
created a file /res/xml/file_paths.xml
and specified path in that
<pathsxmlns:android="http://schemas.android.com/apk/res/android"><external-files-pathname="my_images"path="Pictures" /></paths>
In my activity
created a global variable var cameraPhotoFilePath: Uri? = null
this is how I started Camera acitivity for results
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val photoFile: File? = try {
createImageFileInAppDir()
} catch (ex: IOException) {
// Error occurred while creating the Filenull
}
photoFile?.also { file ->
val photoURI: Uri = FileProvider.getUriForFile(
this,
"com.example.android.provider",
file
)
cameraPhotoFilePath = photoURI
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
startActivityForResult(intent, ACTION_REQUEST_CAMERA)
here is a helper function that i used in above code
@Throws(IOException::class)privatefuncreateImageFileInAppDir(): File {
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val imagePath = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File(imagePath, "JPEG_${timeStamp}_" + ".jpg")
}
At the end in onActivityResult
thats how I got image Uri
if (resultCode == RESULT_OK && requestCode == ACTION_REQUEST_CAMERA) {
cameraPhotoFilePath?.let { uri ->// here uri is image Uri that was captured by camera
}
}
Solution 2:
You should pass your own uri (path) to intent with action MediaStore.ACTION_IMAGE_CAPTURE
putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(
applicationContext,
"$packageName.your_file_provider",
File("path/to")
))
addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
More info about FileProvider
Solution 3:
Android developer have good documentation about how to get full sized photos . You can also get the uri of full size image .Please visit this link Get full size image
Post a Comment for "How To Get Image Uri By Intent Mediastore.action_image_capture In Android 10 And Above"