Do not crash on sending 0 byte videos

java.lang.RuntimeException: setDataSource failed: status = 0xFFFFFFEA
    at android.media.MediaMetadataRetriever._setDataSource(MediaMetadataRetriever.java)
    at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:306)
    at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:327)
    at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:376)
    at org.matrix.android.sdk.internal.session.room.send.LocalEchoEventFactory.createVideoEvent
    at org.matrix.android.sdk.internal.session.room.send.LocalEchoEventFactory.createMediaEvent
    at org.matrix.android.sdk.internal.session.room.send.DefaultSendService.sendMedia(DefaultSendService.kt:253)
    at org.matrix.android.sdk.internal.session.room.send.DefaultSendService.sendMedias(DefaultSendService.kt:240)
    at org.matrix.android.sdk.internal.session.room.DefaultRoom.sendMedias
    at im.vector.app.features.home.room.detail.RoomDetailViewModel.handleSendMedia(RoomDetailViewModel.kt:1158)
    at im.vector.app.features.home.room.detail.RoomDetailViewModel.handle(RoomDetailViewModel.kt:298)
    at im.vector.app.features.home.room.detail.RoomDetailFragment$contentAttachmentActivityResultLauncher$1.invoke(RoomDetailFragment.kt:1161)
    at im.vector.app.features.home.room.detail.RoomDetailFragment$contentAttachmentActivityResultLauncher$1.invoke(RoomDetailFragment.kt:1156)
    at im.vector.app.core.extensions.FragmentKt.registerStartForActivityResult$lambda-0
    at im.vector.app.core.extensions.FragmentKt.lambda$6uyRKSScfR1yqThCkGUu8777SL8
    at im.vector.app.core.extensions.-$$Lambda$FragmentKt$6uyRKSScfR1yqThCkGUu8777SL8.onActivityResult(lambda:0)
    at androidx.activity.result.ActivityResultRegistry$1.onStateChanged(ActivityResultRegistry.java:148)
    at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)
    at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:265)
    at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:307)
    at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148)
    at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)
    at androidx.fragment.app.Fragment.performStart(Fragment.java:3026)
    at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:589)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:300)
    at androidx.fragment.app.FragmentStore.moveToExpectedState
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
    at androidx.fragment.app.FragmentManager.dispatchStart
    at androidx.fragment.app.FragmentController.dispatchStart
    at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:510)
    at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1455)
    at android.app.Activity.performStart(Activity.java:8076)
    at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3653)
    at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
    at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2203)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7822)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

Change-Id: Ib909abe6468703452d53e760bde414e81a8876e3
This commit is contained in:
SpiritCroc 2021-08-20 09:15:58 +02:00
parent 8917e78ced
commit 2f4d379ed0
2 changed files with 12 additions and 2 deletions

View File

@ -58,6 +58,7 @@ import org.matrix.android.sdk.internal.di.UserId
import org.matrix.android.sdk.internal.session.content.ThumbnailExtractor
import org.matrix.android.sdk.internal.session.permalinks.PermalinkFactory
import org.matrix.android.sdk.internal.session.room.send.pills.TextPillsUtils
import java.lang.RuntimeException
import javax.inject.Inject
/**
@ -256,7 +257,11 @@ internal class LocalEchoEventFactory @Inject constructor(
private fun createVideoEvent(roomId: String, attachment: ContentAttachmentData): Event {
val mediaDataRetriever = MediaMetadataRetriever()
mediaDataRetriever.setDataSource(context, attachment.queryUri)
try {
mediaDataRetriever.setDataSource(context, attachment.queryUri)
} catch (e: RuntimeException) {
e.printStackTrace()
}
// Use frame to calculate height and width as we are sure to get the right ones
val firstFrame: Bitmap? = mediaDataRetriever.frameAtTime

View File

@ -23,6 +23,7 @@ import android.provider.MediaStore
import im.vector.lib.multipicker.entity.MultiPickerAudioType
import im.vector.lib.multipicker.entity.MultiPickerImageType
import im.vector.lib.multipicker.entity.MultiPickerVideoType
import java.lang.RuntimeException
internal fun Uri.toMultiPickerImageType(context: Context): MultiPickerImageType? {
val projection = arrayOf(
@ -88,7 +89,11 @@ internal fun Uri.toMultiPickerVideoType(context: Context): MultiPickerVideoType?
context.contentResolver.openFileDescriptor(this, "r")?.use { pfd ->
val mediaMetadataRetriever = MediaMetadataRetriever()
mediaMetadataRetriever.setDataSource(pfd.fileDescriptor)
try {
mediaMetadataRetriever.setDataSource(pfd.fileDescriptor)
} catch (e: RuntimeException) {
e.printStackTrace()
}
duration = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toLong() ?: 0L
width = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)?.toInt() ?: 0
height = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)?.toInt() ?: 0