Mohit Lakhanpal
Mohit Lakhanpal

Reputation: 1505

Take Picture with Camera and set on Image View not working in kotlin

I'm trying to create a camera picker functionality on the app and I face some of the methods of camera picture is deprecated so I trying to resolve the code and not using any depricated method. First on manifest declares the permissions

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

after that create a Camera intent:

private var imageStr:Uri?=null

val values = ContentValues()
values.put(Media.TITLE, "New Picture")
values.put(Media.DESCRIPTION, "From the Camera")
imageStr = requireActivity().contentResolver.insert(EXTERNAL_CONTENT_URI, values)
// camera intent
val intent = Intent(ACTION_IMAGE_CAPTURE)
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageStr)
startForCameraResult.launch(intent)

and after that we get the value on startForCameraResult:

private val startForCameraResult = registerForActivityResult(StartActivityForResult()) {
    if (it.resultCode == RESULT_OK) {
        profileImage.setImageURI(imageStr)
    }
}

Here is the code And The problem is I always get imagestr is null. Please show me the right direction to handle this issue. Thanks

Upvotes: 0

Views: 1075

Answers (1)

Bhavik Nathani
Bhavik Nathani

Reputation: 499

If you are using targetSdkVersion 29 & compileSdkVersion 29 you need to set android:requestLegacyExternalStorage="true" in application tag of AndroidManifest.xml file.

Upvotes: 1

Related Questions