stream-chat-android icon indicating copy to clipboard operation
stream-chat-android copied to clipboard

Crash when attaching photo that doesn't include DURATION column

Open jeff-huston opened this issue 3 years ago • 4 comments

Describe the bug

We're using the system camera to take a photo and place it in the MediaStore. We then send that Uri into the MessageInputView as an attachment, which causes a crash.

This works on several devices we've tried, but on a Pixel 2 Emulator on API 28, the Stream SDK crashes because the photo doesn't have the DURATION column (see stack trace and Stream code snippet below). I'm unsure if this is emulator-specific, device-specific, or OS version-specific, but it seems to me the DURATION column should not be required when attaching a photo. Notably, the crash doesn't occur on a Pixel 2 Emulator on API 29.

You have a different approach in your sample project for capturing a photo that doesn't have this issue, but I'm not sure what's importantly different about the approach I'm taking. And it seems like my approach should be supported as well.

2022-07-27 09:55:35.897 10346-10346/com.myapp.debug E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.myapp.debug, PID: 10346
    android.database.sqlite.SQLiteException: no such column: duration (code 1 SQLITE_ERROR): , while compiling: SELECT _id, _display_name, mime_type, _size, duration FROM images WHERE (_id=?)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
        at android.content.ContentProviderProxy.query(ContentProviderNative.java:418)
        at android.content.ContentResolver.query(ContentResolver.java:802)
        at android.content.ContentResolver.query(ContentResolver.java:752)
        at android.content.ContentResolver.query(ContentResolver.java:710)
        at com.getstream.sdk.chat.utils.StorageHelper.getAttachmentsFromUriList(StorageHelper.kt:110)
        at com.myapp.MessageInputViewUtilsKt$submitDevicePhotos$attachments$1.invokeSuspend(MessageInputViewUtils.kt:110)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
        at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:749)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
    	Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@d98468e, Dispatchers.Main.immediate]
public class StorageHelper {
    // ...
    private val projection = arrayOf(
        MediaStore.Files.FileColumns._ID,
        MediaStore.Files.FileColumns.DISPLAY_NAME,
        MediaStore.Files.FileColumns.MIME_TYPE,
        MediaStore.Files.FileColumns.SIZE,
        MediaStore.Video.Media.DURATION
    )

SDK version

  • 5.5.0

To Reproduce Steps to reproduce the behavior:

  1. Create a File in the cacheDir, and copy it to MediaStore.Images.Media.EXTERNAL_CONTENT_URI (let's call this generated Uri photoUri)
  2. Take a picture using fragment.registerForActivityResult(ActivityResultContracts.TakePicture()) and provide the photoUri from step 1.
  3. After a successful result, use StorageHelper().getAttachmentsFromUriList(context, listOf(photoUri))
  4. App crashes with stacktrace above.

Note that I did try a workaround of inserting MediaStore.Video.Media.DURATION as 0L via ContentValues both when copying the file to MediaStore.Images.Media.EXTERNAL_CONTENT_URI in Step 1, and after getting the result back from the Camera, but neither of those workarounds was effective.

Expected behavior

Photo is inserted into the MessageInputView for attachment when the next message is sent.

Device:

  • Vendor and model: Pixel 2 Emulator
  • Android version: 9.0

Screenshots n/a

jeff-huston avatar Jul 27 '22 16:07 jeff-huston

Hello @hustoj2

Thank you for reporting this. Let me check it.

bychkovdmitry avatar Jul 27 '22 17:07 bychkovdmitry

Thanks @bychkovdmitry.

Update: I've found this isn't specific to photos taken via this camera intent. The same issue occurs on this emulator on API 28 with content:// Uris I get from picking photos from the device's photo library. Even if that specific image in the photo library was synced and downloaded from another device where it was able to be attached.

jeff-huston avatar Jul 27 '22 17:07 jeff-huston

Hello @hustoj2

Sorry for the delayed response. We've made some improvements to the StorageHelper::getAttachmentsFromUriList method in this PR. The PR should fix your issue with the DURATION column. A release with the fix will happen next week, most likely on Monday.

In the meantime, you can can test a snapshot version (5.7.1-SNAPSHOT) and report if the issues are fixed. A guide on how to use snapshots can be found here.

bychkovdmitry avatar Aug 08 '22 12:08 bychkovdmitry

@bychkovdmitry thank you, this is great news! It's not a quick test on my end since there were some code migrations required by 5.6.0 that we haven't done yet. If we can get to that this week, we'll test the snapshot, but from the looks of the change it should work 🎉. Thanks for the quick turnaround on this!

jeff-huston avatar Aug 08 '22 14:08 jeff-huston

hey @hustoj2, I'm going to close this issue as the fix has been shipped in the 5.8.0 release. Let us know if you have any further questions.

adasiewiczr avatar Aug 17 '22 07:08 adasiewiczr

I just tested and am updating to the new version today. It works, thanks!

jeff-huston avatar Aug 17 '22 12:08 jeff-huston