Gligar
Gligar copied to clipboard
Crash on Android 11
Describe the bug Application crashes when gligar activity is launched (After allow permissions for the first time too).
To Reproduce Steps to reproduce the behavior:
- Launch gligar activity.
- Allow permissions.
- App will crash.
Expected behavior Show the list of local image files.
Include stack trace if exist E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant). E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant). E/AndroidRuntime: FATAL EXCEPTION: main Process: avanceingenieros.com.app, PID: 6152 java.lang.IllegalArgumentException: Invalid token LIMIT at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:172) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142) at android.content.ContentProviderProxy.query(ContentProviderNative.java:472) at android.content.ContentResolver.query(ContentResolver.java:1183) at android.content.ContentResolver.query(ContentResolver.java:1115) at android.content.ContentResolver.query(ContentResolver.java:1071) at com.opensooq.supernova.gligar.dataSource.ImagesDataSource.loadAlbumImages(ImagesDataSource.kt:64) at com.opensooq.supernova.gligar.ui.PickerViewModel$getImages$2.invokeSuspend(PickerViewModel.kt:99) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
Smartphone (please complete the following information):
- Device: Pixel 4XL (Emulator)
- OS: Android 11
please help me solve this
You need to remove the LIMIT from ImageDataSource -> loadAlbumImages function
You need to remove the LIMIT from ImageDataSource -> loadAlbumImages function
How to limit users to select only one image from galary.
How to limit users to select only one image from galary. -> you can use new GligarPicker().requestCode(PICKER_REQUEST_CODE).limit(1). limit function like this but it wont help you, stil it is crashing...
Yes, I have tried many ways but I think there is a permission-related issue which must have to fix in the library. Now roll back to own ways of Gallery and Camera taking photos.
I fixed the issues for Android 11, please use this class: internal class ImagesDataSource(private val contentResolver: ContentResolver){
fun getCursorUri(): Uri {
val collection: Uri
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
collection = newCursorUri;
} else {
collection = cursorUri;
}
return collection;
}
fun loadAlbums(): ArrayList<AlbumItem> {
val albumCursor = contentResolver.query(
getCursorUri(),
arrayOf(DISPLAY_NAME_COLUMN, MediaStore.Images.ImageColumns.BUCKET_ID),
null,
null,
ORDER_BY
)
val list = arrayListOf<AlbumItem>()
try {
list.add(AlbumItem("All", true, "0"))
if (albumCursor == null) {
return list
}
albumCursor.doWhile {
try {
val bucketId = albumCursor.getString(albumCursor.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_ID))
val name = albumCursor.getString(albumCursor.getColumnIndex(DISPLAY_NAME_COLUMN))
?: bucketId
var albumItem = AlbumItem(name, false, bucketId)
if (!list.contains(albumItem)) {
list.add(albumItem)
}
}
catch (ex: Exception) {
ex.printStackTrace()
}
}
} finally {
if (albumCursor != null && !albumCursor.isClosed) {
albumCursor.close()
}
}
return list
}
fun loadAlbumImages(
albumItem: AlbumItem?,
page: Int
): ArrayList<ImageItem> {
val offset = page * PAGE_SIZE
val list: ArrayList<ImageItem> = arrayListOf()
var photoCursor: Cursor? = null
try {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
val bundle = Bundle().apply {
putInt(ContentResolver.QUERY_ARG_LIMIT, PAGE_SIZE)
putInt(ContentResolver.QUERY_ARG_OFFSET, offset)
putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, "${MediaStore.MediaColumns.DATE_MODIFIED} DESC")
}
photoCursor = contentResolver.query(
getCursorUri(),
arrayOf(
ID_COLUMN,
PATH_COLUMN
),
bundle,
null
)
}
else {
if (albumItem == null || albumItem.isAll) {
photoCursor = contentResolver.query(
getCursorUri(),
arrayOf(
ID_COLUMN,
PATH_COLUMN
),
null,
null,
"$ORDER_BY LIMIT $PAGE_SIZE OFFSET $offset"
)
} else {
photoCursor = contentResolver.query(
getCursorUri(),
arrayOf(
ID_COLUMN,
PATH_COLUMN
),
"${MediaStore.Images.ImageColumns.BUCKET_ID} =?",
arrayOf(albumItem.bucketId),
"$ORDER_BY LIMIT $PAGE_SIZE OFFSET $offset"
)
}
}
photoCursor?.isAfterLast ?: return list
photoCursor.doWhile {
try {
val image = photoCursor.getString((photoCursor.getColumnIndex(PATH_COLUMN)))
list.add(ImageItem(image, ImageSource.GALLERY, 0))
}
catch (ex: Exception) {
ex.printStackTrace()
}
}
} finally {
if (photoCursor != null) {
if (!photoCursor.isClosed()) {
photoCursor.close()
}
}
}
return list
}
}
@Shay-BH Thanks its working now :)
Hello, Can you guide me how to edit "ImagesDataSource" file. Its showing file is read only.
@Shay-BH What to place in "newCursorUri" in Constant
Hi @Shay-BH @sumeet3110. Can you please guide me what to place as newCursorUri. Thanks!
@HassanGlixenTech you need to place it in Const.kt
@HassanGlixenTech you need to place it in Const.kt @AlexStroia What could be the value of that constant. If you can guide me that would be really helpful as i am newbie at this topic.
@ateeqrehman33 Hi you have to make this library as a dependency to your project (Download it and add it your project) then you will able to edit ImageDataSource file..
@ateeqrehman33 I already have access to ImageDataSource. I just need to find out what can be the value placed at this position
@HassanGlixenTech I have used the same as cursorUri and it is working for me .
Still getting this error, has it been FIXED ?
Kindly merge the pull request Yazan98 this user already fixed all the issues. My bad luck i am using it in my main application. For now i import it as a module and fixed it on my side but its a temporary fix that i did .. i am waiting for your update.
Thanks
Kindly merge the pull request Yazan98 this user already fixed all the issues. My bad luck i am using it in my main application. For now i import it as a module and fixed it on my side but its a temporary fix that i did .. i am waiting for your update.
Thanks
Please tell me the solution to this problem. I could not solve the problem with what was said
I fixed the issues for Android 11, please use this class: internal class ImagesDataSource(private val contentResolver: ContentResolver){
fun getCursorUri(): Uri { val collection: Uri if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { collection = newCursorUri; } else { collection = cursorUri; } return collection; } fun loadAlbums(): ArrayList<AlbumItem> { val albumCursor = contentResolver.query( getCursorUri(), arrayOf(DISPLAY_NAME_COLUMN, MediaStore.Images.ImageColumns.BUCKET_ID), null, null, ORDER_BY ) val list = arrayListOf<AlbumItem>() try { list.add(AlbumItem("All", true, "0")) if (albumCursor == null) { return list } albumCursor.doWhile { try { val bucketId = albumCursor.getString(albumCursor.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_ID)) val name = albumCursor.getString(albumCursor.getColumnIndex(DISPLAY_NAME_COLUMN)) ?: bucketId var albumItem = AlbumItem(name, false, bucketId) if (!list.contains(albumItem)) { list.add(albumItem) } } catch (ex: Exception) { ex.printStackTrace() } } } finally { if (albumCursor != null && !albumCursor.isClosed) { albumCursor.close() } } return list } fun loadAlbumImages( albumItem: AlbumItem?, page: Int ): ArrayList<ImageItem> { val offset = page * PAGE_SIZE val list: ArrayList<ImageItem> = arrayListOf() var photoCursor: Cursor? = null try { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) { val bundle = Bundle().apply { putInt(ContentResolver.QUERY_ARG_LIMIT, PAGE_SIZE) putInt(ContentResolver.QUERY_ARG_OFFSET, offset) putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, "${MediaStore.MediaColumns.DATE_MODIFIED} DESC") } photoCursor = contentResolver.query( getCursorUri(), arrayOf( ID_COLUMN, PATH_COLUMN ), bundle, null ) } else { if (albumItem == null || albumItem.isAll) { photoCursor = contentResolver.query( getCursorUri(), arrayOf( ID_COLUMN, PATH_COLUMN ), null, null, "$ORDER_BY LIMIT $PAGE_SIZE OFFSET $offset" ) } else { photoCursor = contentResolver.query( getCursorUri(), arrayOf( ID_COLUMN, PATH_COLUMN ), "${MediaStore.Images.ImageColumns.BUCKET_ID} =?", arrayOf(albumItem.bucketId), "$ORDER_BY LIMIT $PAGE_SIZE OFFSET $offset" ) } } photoCursor?.isAfterLast ?: return list photoCursor.doWhile { try { val image = photoCursor.getString((photoCursor.getColumnIndex(PATH_COLUMN))) list.add(ImageItem(image, ImageSource.GALLERY, 0)) } catch (ex: Exception) { ex.printStackTrace() } } } finally { if (photoCursor != null) { if (!photoCursor.isClosed()) { photoCursor.close() } } } return list }
}
Thanks and It is working
I could not add this project as a package to my project so I could edit it! Please explain how to add and edit this project to our project ...
Use
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
implementation 'com.github.omkar-tenkale:Gligar:78d8110102e0be4e1e2f939b5307ae5f645e6761'
instead of
implementation 'com.opensooq.supernova:gligar:1.1.0'
Based on pull request https://github.com/OpenSooq/Gligar/pull/25
Use
implementation 'com.github.omkar-tenkale:Gligar:78d8110102e0be4e1e2f939b5307ae5f645e6761'
instead of
implementation 'com.opensooq.supernova:gligar:1.1.0'
Based on pull request #25
thank you I tried your method But again an error occurred And the program crashed
try File > Invalid caches/Restart
I have same problem
E/AndroidRuntime: FATAL EXCEPTION: main Process: it.nexid.ivoting, PID: 11134 java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/lifecycle/ViewModelKt; at com.opensooq.supernova.gligar.ui.PickerViewModel.loadAlbums$gligar_release(PickerViewModel.kt:80) at com.opensooq.supernova.gligar.ui.ImagePickerActivity.loadAlbums(ImagePickerActivity.kt:137) at com.opensooq.supernova.gligar.ui.ImagePickerActivity.storagePermissionGranted(ImagePickerActivity.kt:142) at com.opensooq.supernova.gligar.ui.ImagePickerActivity.checkStoragePermission(ImagePickerActivity.kt:190) at com.opensooq.supernova.gligar.ui.ImagePickerActivity.onResume(ImagePickerActivity.kt:406) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1456) at android.app.Activity.performResume(Activity.java:8353) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4889) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4936) at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52) at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2317) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:246) at android.app.ActivityThread.main(ActivityThread.java:8595) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.lifecycle.ViewModelKt" on path: DexPathList[[zip file "/data/app/~~TKOg2FjMjw4LEPP7lLtf9w==/it.nexid.ivoting-mr-dzpUGyp5Y-8l8aVapFw==/base.apk"],nativeLibraryDirectories=[/data/app/~~TKOg2FjMjw4LEPP7lLtf9w==/it.nexid.ivoting-mr-dzpUGyp5Y-8l8aVapFw==/lib/arm64, /system/lib64, /system/system_ext/lib64]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207) at java.lang.ClassLoader.loadClass(ClassLoader.java:379) at java.lang.ClassLoader.loadClass(ClassLoader.java:312) at com.opensooq.supernova.gligar.ui.PickerViewModel.loadAlbums$gligar_release(PickerViewModel.kt:80)Â at com.opensooq.supernova.gligar.ui.ImagePickerActivity.loadAlbums(ImagePickerActivity.kt:137)Â at com.opensooq.supernova.gligar.ui.ImagePickerActivity.storagePermissionGranted(ImagePickerActivity.kt:142)Â at com.opensooq.supernova.gligar.ui.ImagePickerActivity.checkStoragePermission(ImagePickerActivity.kt:190)Â at com.opensooq.supernova.gligar.ui.ImagePickerActivity.onResume(ImagePickerActivity.kt:406)Â at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1456)Â at android.app.Activity.performResume(Activity.java:8353)Â at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4889)Â at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4936)Â at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)Â at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)Â at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)Â at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2317)Â at android.os.Handler.dispatchMessage(Handler.java:106)Â at android.os.Looper.loop(Looper.java:246)Â at android.app.ActivityThread.main(ActivityThread.java:8595)Â at java.lang.reflect.Method.invoke(Native Method)Â at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)Â at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)Â V/FA: Connection attempt already in progress V/FA: Connection attempt already in progress I/Process: Sending signal. PID: 11134 SIG: 9
i try
File > Invalid caches/Restart but no effect
try File > Invalid caches/Restart
I tried this The program crashed again
Regarding crash java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/lifecycle/ViewModelKt; at
This doesnt occur to me nor it seems related to lib
you can try Add the dependencies in the build.gradle of the app in my scenario was using a library that needed those dependencies
For android X
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
Make sure android X is enabled with gradle.properties and Jetfier enabled
android.useAndroidX=true
android.enableJetifier=true
https://stackoverflow.com/questions/44075089/noclassdeffounderror-landroid-arch-lifecycle-lifecycledispatcher
I fixed the issues for Android 11, please use this class: internal class ImagesDataSource(private val contentResolver: ContentResolver){
fun getCursorUri(): Uri { val collection: Uri if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { collection = newCursorUri; } else { collection = cursorUri; } return collection; } fun loadAlbums(): ArrayList<AlbumItem> { val albumCursor = contentResolver.query( getCursorUri(), arrayOf(DISPLAY_NAME_COLUMN, MediaStore.Images.ImageColumns.BUCKET_ID), null, null, ORDER_BY ) val list = arrayListOf<AlbumItem>() try { list.add(AlbumItem("All", true, "0")) if (albumCursor == null) { return list } albumCursor.doWhile { try { val bucketId = albumCursor.getString(albumCursor.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_ID)) val name = albumCursor.getString(albumCursor.getColumnIndex(DISPLAY_NAME_COLUMN)) ?: bucketId var albumItem = AlbumItem(name, false, bucketId) if (!list.contains(albumItem)) { list.add(albumItem) } } catch (ex: Exception) { ex.printStackTrace() } } } finally { if (albumCursor != null && !albumCursor.isClosed) { albumCursor.close() } } return list } fun loadAlbumImages( albumItem: AlbumItem?, page: Int ): ArrayList<ImageItem> { val offset = page * PAGE_SIZE val list: ArrayList<ImageItem> = arrayListOf() var photoCursor: Cursor? = null try { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) { val bundle = Bundle().apply { putInt(ContentResolver.QUERY_ARG_LIMIT, PAGE_SIZE) putInt(ContentResolver.QUERY_ARG_OFFSET, offset) putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, "${MediaStore.MediaColumns.DATE_MODIFIED} DESC") } photoCursor = contentResolver.query( getCursorUri(), arrayOf( ID_COLUMN, PATH_COLUMN ), bundle, null ) } else { if (albumItem == null || albumItem.isAll) { photoCursor = contentResolver.query( getCursorUri(), arrayOf( ID_COLUMN, PATH_COLUMN ), null, null, "$ORDER_BY LIMIT $PAGE_SIZE OFFSET $offset" ) } else { photoCursor = contentResolver.query( getCursorUri(), arrayOf( ID_COLUMN, PATH_COLUMN ), "${MediaStore.Images.ImageColumns.BUCKET_ID} =?", arrayOf(albumItem.bucketId), "$ORDER_BY LIMIT $PAGE_SIZE OFFSET $offset" ) } } photoCursor?.isAfterLast ?: return list photoCursor.doWhile { try { val image = photoCursor.getString((photoCursor.getColumnIndex(PATH_COLUMN))) list.add(ImageItem(image, ImageSource.GALLERY, 0)) } catch (ex: Exception) { ex.printStackTrace() } } } finally { if (photoCursor != null) { if (!photoCursor.isClosed()) { photoCursor.close() } } } return list }
}
This would fix the crash for android 11, but you won't be able to switch albums.
Use this function instead. You will be able to switch albums.
fun loadAlbumImages( albumItem: AlbumItem?, page: Int ): ArrayList<ImageItem> { val offset = page * PAGE_SIZE val list: ArrayList<ImageItem> = arrayListOf() var photoCursor: Cursor? = null try { val selection = "${MediaStore.Images.ImageColumns.BUCKET_ID} =?" if (albumItem == null || albumItem.isAll) { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) { val bundle = Bundle().apply { putInt(ContentResolver.QUERY_ARG_LIMIT, PAGE_SIZE) putInt(ContentResolver.QUERY_ARG_OFFSET, offset) putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, "${MediaStore.MediaColumns.DATE_MODIFIED} DESC") } photoCursor = contentResolver.query( cursorUri, arrayOf( ID_COLUMN, PATH_COLUMN ), bundle, null ) } else { photoCursor = contentResolver.query( cursorUri, arrayOf( ID_COLUMN, PATH_COLUMN ), null, null, "$ORDER_BY LIMIT $PAGE_SIZE OFFSET $offset" ) } } else { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) { val bundle = Bundle().apply { putInt(ContentResolver.QUERY_ARG_LIMIT, PAGE_SIZE) putInt(ContentResolver.QUERY_ARG_OFFSET, offset) putString(ContentResolver.QUERY_ARG_SQL_SORT_ORDER, "${MediaStore.MediaColumns.DATE_MODIFIED} DESC") putString(ContentResolver.QUERY_ARG_SQL_SELECTION, selection) putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, arrayOf(albumItem.bucketId)) } photoCursor = contentResolver.query( cursorUri, arrayOf( ID_COLUMN, PATH_COLUMN ), bundle, null ) } else { photoCursor = contentResolver.query( cursorUri, arrayOf( ID_COLUMN, PATH_COLUMN ), selection, arrayOf(albumItem.bucketId), "$ORDER_BY LIMIT $PAGE_SIZE OFFSET $offset" ) } } photoCursor?.isAfterLast ?: return list photoCursor.doWhile { val image = photoCursor.getString((photoCursor.getColumnIndex(PATH_COLUMN))) list.add(ImageItem(image, ImageSource.GALLERY, 0)) } } finally { if (photoCursor != null && !photoCursor.isClosed()) { photoCursor.close() } } return list }
Use
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
implementation 'com.github.omkar-tenkale:Gligar:78d8110102e0be4e1e2f939b5307ae5f645e6761'
instead of
implementation 'com.opensooq.supernova:gligar:1.1.0'
Based on pull request #25
It runs, Thanks a much