Skip to content

Commit

Permalink
Fix permission error on API 33
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhoanglam committed Nov 26, 2022
1 parent 3af5910 commit 36a41e3
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 72 deletions.
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Preview

Changelog
--------
- Fix empty images bug.
- Upgrade some dependency versions: Kotlin v1.7.0, Glide v4.13.2 ...
- Fix permission error when running on Android 13.
- Upgrade some dependency versions.

Installation
--------
Expand All @@ -33,7 +33,7 @@ dependencyResolutionManagement {
Add the following dependency in app build.gradle:
```
dependencies {
implementation 'com.github.nguyenhoanglam:ImagePicker:1.5.3'
implementation 'com.github.nguyenhoanglam:ImagePicker:1.5.4'
}
```

Expand All @@ -51,14 +51,14 @@ Usage
Define an `ActivityResultLauncher` class variable in `Activity` or `Fragment`.
```java
private val launcher = registerImagePicker { images ->
// Selected images are ready to use
if(images.isNotEmpty()){
// Selected images are ready to use
if(images.isNotEmpty()){
val sampleImage = images[0]
Glide.with(this@MainActivity)
.load(sampleImage.uri)
.into(imageView)
}
}
.load(sampleImage.uri)
.into(imageView)
}
}
```

Then, launch image picker when needed.
Expand All @@ -69,19 +69,19 @@ launcher.launch()
- With customize configuration:
```java
val config = ImagePickerConfig(
statusBarColor = "#000000",
isLightStatusBar = false,
isFolderMode = true,
isMultipleMode = true,
maxSize = 10,
rootDirectory = Config.ROOT_DIR_DOWNLOAD,
subDirectory = "Photos",
folderGridCount = GridCount(2, 4),
imageGridCount = GridCount(3, 5),
// See more at configuration attributes table below
)

launcher.launch(config)
statusBarColor = "#000000",
isLightStatusBar = false,
isFolderMode = true,
isMultipleMode = true,
maxSize = 10,
rootDirectory = Config.ROOT_DIR_DOWNLOAD,
subDirectory = "Photos",
folderGridCount = GridCount(2, 4),
imageGridCount = GridCount(3, 5),
// See more at configuration attributes table below
)

launcher.launch(config)
```

Configuration attributes
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.7.0'
ext.kotlin_version = '1.7.20'
repositories {
mavenCentral()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.1'
classpath 'com.android.tools.build:gradle:7.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

plugins {
id 'org.jetbrains.kotlin.jvm' version '1.7.0'
id 'org.jetbrains.kotlin.jvm' version '1.7.20'
}

allprojects {
Expand Down
4 changes: 2 additions & 2 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'com.github.bumptech.glide:glide:4.13.2'
implementation 'com.github.bumptech.glide:glide:4.14.2'
implementation project(path: ':imagepicker')
}
16 changes: 9 additions & 7 deletions imagepicker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation "androidx.activity:activity:1.7.0-alpha02"
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.0'
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.0'
implementation 'com.google.android.material:material:1.6.1'
implementation 'com.github.bumptech.glide:glide:4.13.2'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20'
implementation 'com.google.android.material:material:1.7.0'
implementation 'com.github.bumptech.glide:glide:4.14.2'
annotationProcessor 'com.github.bumptech.glide:compiler:4.14.2'
}

afterEvaluate {
Expand All @@ -45,7 +47,7 @@ afterEvaluate {
// You can then customize attributes of the publication as shown below.
groupId = 'com.github.nguyenhoanglam'
artifactId = 'final'
version = '1.5.3'
version = '1.5.4'
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions imagepicker/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.nguyenhoanglam.imagepicker">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
Expand All @@ -13,7 +17,9 @@
</intent>
</queries>

<application>
<application
android:enableOnBackInvokedCallback="true"
tools:targetApi="tiramisu">

<activity
android:name=".ui.imagepicker.ImagePickerActivity"
Expand All @@ -30,7 +36,6 @@
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ package com.nguyenhoanglam.imagepicker.helper
object Constants {
const val EXTRA_CONFIG = "ImagePickerConfig"
const val EXTRA_IMAGES = "ImagePickerImages"
const val RC_READ_EXTERNAL_STORAGE_PERMISSION = 1000
const val RC_WRITE_EXTERNAL_STORAGE_PERMISSION = 1001
const val RC_READ_PERMISSION = 1000
const val RC_WRITE_PERMISSION = 1001
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ object DeviceHelper {

val isMinSdk29 get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q

val isMinSdk33 get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU

fun checkCameraAvailability(context: Context): Boolean {
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val isAvailable = intent.resolveActivity(context.packageManager) != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ object PermissionHelper {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
}

fun shouldShowRequestPermissionRationale(activity: Activity?, permission: String): Boolean {
private fun shouldShowRequestPermissionRationale(activity: Activity?, permission: String): Boolean {
if (activity != null) {
return ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import com.nguyenhoanglam.imagepicker.helper.PermissionHelper.openAppSettings
import com.nguyenhoanglam.imagepicker.helper.ToastHelper
import com.nguyenhoanglam.imagepicker.model.Image
import com.nguyenhoanglam.imagepicker.model.ImagePickerConfig
import java.util.*

class CameraActivity : AppCompatActivity() {

Expand Down Expand Up @@ -64,7 +63,11 @@ class CameraActivity : AppCompatActivity() {
return
}

config = intent.getParcelableExtra(Constants.EXTRA_CONFIG)!!
@Suppress("DEPRECATION")
config = if (DeviceHelper.isMinSdk33) intent.getParcelableExtra(
Constants.EXTRA_CONFIG,
ImagePickerConfig::class.java
)!! else intent.getParcelableExtra(Constants.EXTRA_CONFIG)!!
config.initDefaultValues(this@CameraActivity)

binding = ImagepickerActivityCameraBinding.inflate(layoutInflater)
Expand Down Expand Up @@ -93,15 +96,15 @@ class CameraActivity : AppCompatActivity() {
PermissionHelper.requestAllPermissions(
this@CameraActivity,
permissions,
Constants.RC_WRITE_EXTERNAL_STORAGE_PERMISSION
Constants.RC_WRITE_PERMISSION
)
}

override fun onPermissionPreviouslyDenied() {
PermissionHelper.requestAllPermissions(
this@CameraActivity,
permissions,
Constants.RC_WRITE_EXTERNAL_STORAGE_PERMISSION
Constants.RC_WRITE_PERMISSION
)
}

Expand Down Expand Up @@ -159,7 +162,7 @@ class CameraActivity : AppCompatActivity() {
grantResults: IntArray
) {
when (requestCode) {
Constants.RC_WRITE_EXTERNAL_STORAGE_PERMISSION -> {
Constants.RC_WRITE_PERMISSION -> {
if (hasGranted(grantResults)) {
captureImage()
} else {
Expand Down
Loading

0 comments on commit 36a41e3

Please sign in to comment.