aloisdeniel / media_gallery

A Flutter plugin that lists native gallery items.
MIT License
86 stars 33 forks source link

App Crash on Android Device (Emulator Android 10 Api 29, Google Pixel API 29) #8

Closed Letalus closed 4 years ago

Letalus commented 4 years ago

Hey whenever I try to get the Medias from my phones the app crashes with following stack trace:

E/AndroidRuntime(25416): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(25416): Process: com.app.dayblizz, PID: 25416
E/AndroidRuntime(25416): java.lang.RuntimeException: An error occurred while executing doInBackground()
E/AndroidRuntime(25416):    at android.os.AsyncTask$4.done(AsyncTask.java:399)
E/AndroidRuntime(25416):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
E/AndroidRuntime(25416):    at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
E/AndroidRuntime(25416):    at java.util.concurrent.FutureTask.run(FutureTask.java:271)
E/AndroidRuntime(25416):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289)
E/AndroidRuntime(25416):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E/AndroidRuntime(25416):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E/AndroidRuntime(25416):    at java.lang.Thread.run(Thread.java:919)
E/AndroidRuntime(25416): Caused by: java.lang.IllegalArgumentException: Invalid column count(*) AS count
E/AndroidRuntime(25416):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:170)
E/AndroidRuntime(25416):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
E/AndroidRuntime(25416):    at android.content.ContentProviderProxy.query(ContentProviderNative.java:423)
E/AndroidRuntime(25416):    at android.content.ContentResolver.query(ContentResolver.java:944)
E/AndroidRuntime(25416):    at android.content.ContentResolver.query(ContentResolver.java:880)
E/AndroidRuntime(25416):    at android.content.ContentResolver.query(ContentResolver.java:836)
E/AndroidRuntime(25416):    at com.example.media_gallery.MediaGalleryPlugin.listImages(MediaGalleryPlugin.kt:233)
E/AndroidRuntime(25416):    at com.example.media_gallery.MediaGalleryPlugin.access$listImages(MediaGalleryPlugin.kt:18)
E/AndroidRuntime(25416):    at com.example.media_gallery.MediaGalleryPlugin$onMethodCall$3.invoke(MediaGalleryPlugin.kt:66)
E/AndroidRuntime(25416):    at com.example.media_gallery.MediaGalleryPlugin$onMethodCall$3.invoke(MediaGalleryPlugin.kt:18)
E/AndroidRuntime(25416):    at com.example.media_gallery.doAsync.doInBackground(MediaGalleryPlugin.kt:531)
E/AndroidRuntime(25416):    at com.example.media_gallery.doAsync.doInBackground(MediaGalleryPlugin.kt:525)
E/AndroidRuntime(25416):    at android.os.AsyncTask$3.call(AsyncTask.java:378)
E/AndroidRuntime(25416):    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
E/AndroidRuntime(25416):    ... 4 more
I/Process (25416): Sending signal. PID: 25416 SIG: 9

Apparently the error happens due to this error : Invalid column count(*) AS count.

IMPORTANT

It seems after some testing that this error only appears with versions Android APi 29 and plus. The crashes happen probably due to one of this Android changes

Does someone know how to fix this?

aloisdeniel commented 4 years ago

The interesting part from the release note :

External storage access scoped to app files and media

By default, apps targeting Android 10 and higher are given scoped access into external storage, or scoped storage. Such apps can see the following types of files within an external storage device without needing to request any storage-related user permissions:

Files in the app-specific directory, accessed using getExternalFilesDir(). Photos, videos, and audio clips that the app created from the media store. To learn more about scoped storage, as well as how to share, access, and modify files that are saved on external storage devices, see the guides on how to manage files in external storage and access and modify media files.

But from my understanding, it should still work the same way if you request permissions...

Some articles that may help tackle the issue :

Letalus commented 4 years ago

Hey aloisdeniel, thanks for your quick reply. I believe you are right, that the permission are not the reason for the crash. I call this setGalleryImageList() methode in the initState of the screen widget. Somehow this code code throws the error above. Can you maybe see what Im doing wrong there or what's going wrong there? Maybe it fails because it is called within the initState but that would be a really super weird bug.

Screenshot 2020-07-08 at 11 08 11 Screenshot 2020-07-08 at 11 15 06
Letalus commented 4 years ago

okay I finally found the reason why I get the above error: Its due to my app/build.gradle file. Within the app/build.gradle of the example project the variables "complieSdkVersion" and "targetSdkVersion" is set to 28. After you have upgraded those gradles to 29 you will get this exact error. To use the current plugin, I had to downgrade those variables from 29 to 28.

Screenshot 2020-07-18 at 21 55 40

As an improvement for the package, I suggest to update the project complieSdkVersion (within the android.build.gradle) to 29, so this error will disappear. Screenshot 2020-07-18 at 22 00 09

aloisdeniel commented 4 years ago

Thanks for the resolution details!

I'll see if I can upgrade the compileSdkVersion to 29.