android / codelab-android-workmanager

Apache License 2.0
551 stars 239 forks source link

Android 8.1.0 "API 27" (Permision denial: android.permission.WRITE_EXTERNAL_STORAGE don't work) #307

Open SonOfaSleep opened 2 years ago

SonOfaSleep commented 2 years ago

I catch an error on API 27:

2022-10-05 13:49:41.061 16988-17081/com.example.background E/MediaStore: Failed to insert image java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=16988, uid=10116 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission() at android.os.Parcel.readException(Parcel.java:2013) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476) at android.content.ContentResolver.insert(ContentResolver.java:1539) at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:993) at com.example.background.workers.SaveImageToFileWorker.doWork(SaveImageToFileWorker.kt:40) at androidx.work.Worker$1.run(Worker.java:86) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764) 2022-10-05 13:49:41.061 16988-17081/com.example.background E/SaveImageToFileWorker: Writing to MediaStore failed

This permission is mensioned in manifest (see screenshot)

зображення

How to fix this error on API 27?

Kakaranara commented 2 years ago

I catch an error on API 27:

2022-10-05 13:49:41.061 16988-17081/com.example.background E/MediaStore: Failed to insert image java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=16988, uid=10116 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission() at android.os.Parcel.readException(Parcel.java:2013) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476) at android.content.ContentResolver.insert(ContentResolver.java:1539) at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:993) at com.example.background.workers.SaveImageToFileWorker.doWork(SaveImageToFileWorker.kt:40) at androidx.work.Worker$1.run(Worker.java:86) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764) 2022-10-05 13:49:41.061 16988-17081/com.example.background E/SaveImageToFileWorker: Writing to MediaStore failed

This permission is mensioned in manifest (see screenshot) зображення

How to fix this error on API 27?

you should make a runtime permission about it.

companion object {
    private const val permission = Manifest.permission.WRITE_EXTERNAL_STORAGE
}
val launcher = registerForActivityResult(ActivityResultContracts.RequestPermission()) {}