Skip to content

Commit

Permalink
Fixed app crash issue due to Camera Permission
Browse files Browse the repository at this point in the history
Fixed ImagePicker crash issue while capturing Camera Image and Camera Permission is Defined in Manifest

#34
  • Loading branch information
Dhaval2404 committed Oct 12, 2019
1 parent aa82942 commit 819ec4f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 22 deletions.
4 changes: 2 additions & 2 deletions imagepicker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ apply plugin: 'kotlin-android-extensions'
apply from: "../ktlint.gradle"

android {
compileSdkVersion 29
compileSdkVersion 28


defaultConfig {
minSdkVersion 19
targetSdkVersion 29
targetSdkVersion 28
versionCode 5
versionName "1.4"

Expand Down
3 changes: 2 additions & 1 deletion imagepicker/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

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

<application android:requestLegacyExternalStorage="true">
<application>

<activity android:name=".ImagePickerActivity"
android:theme="@style/Theme.Transparent.ImagePicker"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.dhaval2404.imagepicker.provider

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.core.app.ActivityCompat.requestPermissions
import com.github.dhaval2404.imagepicker.ImagePickerActivity
Expand Down Expand Up @@ -47,6 +48,12 @@ class CameraProvider(activity: ImagePickerActivity) : BaseProvider(activity) {
*/
private var mCameraFile: File? = null

/**
* True If Camera Permission Defined in AndroidManifest.xml
*/
private val mAskCameraPermission = PermissionUtil
.isPermissionInManifest(this, Manifest.permission.CAMERA)

/**
* Start Camera Capture Intent
*/
Expand All @@ -60,17 +67,12 @@ class CameraProvider(activity: ImagePickerActivity) : BaseProvider(activity) {
* If permission is not granted request Permission, Else start Camera Intent
*/
private fun checkPermission() {
//Check if Camera permission defined in manifest
val requireCameraPermission = PermissionUtil.isPermissionInManifest(this, Manifest.permission.CAMERA)
if (requireCameraPermission && !isPermissionGranted(this, REQUIRED_PERMISSIONS_EXTENDED)) {
//If Camera permission defined in AndroidManifest then Need to request Camera Permission
//Ref: https://github.com/Dhaval2404/ImagePicker/issues/34
requestPermissions(activity, REQUIRED_PERMISSIONS_EXTENDED, PERMISSION_INTENT_REQ_CODE)
} else if (!isPermissionGranted(this, REQUIRED_PERMISSIONS)) {
//If Camera permission is not defined in AndroidManifest then no need to request Camera Permission
requestPermissions(activity, REQUIRED_PERMISSIONS, PERMISSION_INTENT_REQ_CODE)
} else {
if (isPermissionGranted(this)) {
// Permission Granted, Start Camera Intent
startCameraIntent()
} else {
// Request Permission
requestPermission()
}
}

Expand Down Expand Up @@ -98,13 +100,17 @@ class CameraProvider(activity: ImagePickerActivity) : BaseProvider(activity) {
fun onRequestPermissionsResult(requestCode: Int) {
if (requestCode == PERMISSION_INTENT_REQ_CODE) {
// Check again if permission is granted
if (isPermissionGranted(this, REQUIRED_PERMISSIONS)) {
if (isPermissionGranted(this)) {
// Permission is granted, Start Camera Intent
startCameraIntent()
} else {
// Exit with error message
val error = getString(R.string.permission_camera_denied)
setError(error)
val errorRes = if (mAskCameraPermission) {
R.string.permission_camera_extended_denied
} else {
R.string.permission_camera_denied
}
setError(getString(errorRes))
}
}
}
Expand Down Expand Up @@ -139,4 +145,39 @@ class CameraProvider(activity: ImagePickerActivity) : BaseProvider(activity) {
override fun onFailure() {
mCameraFile?.delete()
}
}

/**
* Request Runtime Permission required for Taking Pictures.
* Ref: https://github.com/Dhaval2404/ImagePicker/issues/34
*/
private fun requestPermission() {
if (mAskCameraPermission) {
// If Camera permission defined in AndroidManifest then Need to request Camera Permission
// Ref: https://github.com/Dhaval2404/ImagePicker/issues/34
requestPermissions(activity, REQUIRED_PERMISSIONS_EXTENDED, PERMISSION_INTENT_REQ_CODE)
} else {
// If Camera permission is not defined in AndroidManifest then no need to request Camera Permission
requestPermissions(activity, REQUIRED_PERMISSIONS, PERMISSION_INTENT_REQ_CODE)
}
}

/**
* Check if Check Require permission granted for Taking Picture.
* Ref: https://github.com/Dhaval2404/ImagePicker/issues/34
*
* @param context Application Context
* @return boolean true if all required permission granted else false.
*/
private fun isPermissionGranted(context: Context): Boolean {
// Check if Camera permission defined in manifest
if (mAskCameraPermission && isPermissionGranted(context, REQUIRED_PERMISSIONS_EXTENDED)) {
// Camera and Storage permission is granted
return true
} else if (!mAskCameraPermission && isPermissionGranted(context, REQUIRED_PERMISSIONS)) {
// Storage permission is granted
return true
}
return false
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ object PermissionUtil {
*
* @return true if permission defined in AndroidManifest.xml file, else return false.
*/
fun isPermissionInManifest(context: Context, permission: String) : Boolean {
fun isPermissionInManifest(context: Context, permission: String): Boolean {
val packageInfo = context.packageManager.getPackageInfo(context.packageName, PackageManager.GET_PERMISSIONS)
val permissions = packageInfo.requestedPermissions

Expand Down
6 changes: 4 additions & 2 deletions imagepicker/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
<string name="message_gallery_permission_denied">Read External Storage permissions is needed to select Image. Please turn on requested permissions from Settings > Permissions.</string>
<string name="message_camera_permission_denied">Camera and External Storage permissions are needed to capture image. Turning off any permission won\'t allow application run smoothly. Please turn on all requested permissions from Settings > Permissions.</string>
<string name="permission_gallery_denied">
<![CDATA[Storage permissions are needed to Pick Gallery Image. Please turn on requested permissions from Settings > Permissions.]]></string>
<![CDATA[Storage permission is needed to pick gallery image. Please allow storage permission from Settings > Permissions.]]></string>
<string name="permission_camera_extended_denied">
<![CDATA[Camera and Storage permissions are needed to take a picture. Please allow both the requested permissions from Settings > Permissions.]]></string>
<string name="permission_camera_denied">
<![CDATA[Camera and Storage permissions are needed to capture image. Please all both requested permissions from Settings > Permissions.]]></string>
<![CDATA[Storage permission is needed to take a picture. Please allow storage permission from Settings > Permissions.]]></string>

<string name="error_failed_to_create_camera_image_file">Failed to create Camera image file</string>
<string name="error_failed_pick_gallery_image">Failed to pick Gallery image</string>
Expand Down
4 changes: 2 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ apply plugin: 'kotlin-android-extensions'
apply from: "../ktlint.gradle"

android {
compileSdkVersion 29
compileSdkVersion 28
defaultConfig {
applicationId "com.github.dhaval2404.imagepicker.sample"
minSdkVersion 19
targetSdkVersion 29
targetSdkVersion 28
versionCode 5
versionName "1.4"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down

0 comments on commit 819ec4f

Please sign in to comment.