游标抛出IllegalStateException:未知URL:启动设备上的content:// media / external

时间:2018-09-24 14:06:32

标签: android kotlin android-camera android-cursor contentobserver

我只有在启动设备时才出现此错误,在模拟器上我没有任何问题,现在我在选项卡S2(7.0)和选项卡S3(8.0)上看到了此问题,但在选项卡活动2(7.1),这使它更加奇怪。

这是堆栈跟踪:

    09-24 15:53:03.651 com.insypro.inspector3.debug W/System.err: java.lang.IllegalStateException: Unknown URL: content://media/external
09-24 15:53:03.657 com.insypro.inspector3.debug W/System.err:     at android.os.Parcel.readException(Parcel.java:1701)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
        at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
        at android.content.ContentResolver.query(ContentResolver.java:536)
        at android.content.ContentResolver.query(ContentResolver.java:478)
        at com.insypro.inspector3.utils.PictureObserver$observe$1.apply(PictureObserver.kt:22)
        at com.insypro.inspector3.utils.PictureObserver$observe$1.apply(PictureObserver.kt:13)
        at io.reactivex.internal.operators.flowable.FlowableMap$MapSubscriber.poll(FlowableMap.java:81)
        at io.reactivex.internal.operators.flowable.FlowableConcatMap$ConcatMapImmediate.drain(FlowableConcatMap.java:266)
        at io.reactivex.internal.operators.flowable.FlowableConcatMap$BaseConcatMapSubscriber.onNext(FlowableConcatMap.java:159)
        at io.reactivex.internal.operators.flowable.FlowableMap$MapSubscriber.onNext(FlowableMap.java:57)
        at io.reactivex.internal.operators.flowable.FlowableOnBackpressureBuffer$BackpressureBufferSubscriber.onNext(FlowableOnBackpressureBuffer.java:110)
        at io.reactivex.processors.PublishProcessor$PublishSubscription.onNext(PublishProcessor.java:311)
        at io.reactivex.processors.PublishProcessor.onNext(PublishProcessor.java:195)
        at com.insypro.inspector3.utils.PictureObserver.onChange(PictureObserver.kt:48)
        at android.database.ContentObserver.onChange(ContentObserver.java:145)
        at android.database.ContentObserver.dispatchChange(ContentObserver.java:196)
        at android.database.ContentObserver.-wrap0(ContentObserver.java)
        at android.database.ContentObserver$Transport.onChange(ContentObserver.java:231)
09-24 15:53:03.658 com.insypro.inspector3.debug W/System.err:     at android.database.IContentObserver$Stub.onTransact(IContentObserver.java:62)
        at android.os.Binder.execTransact(Binder.java:573)

基本上,我正在使用contentobserver来监听拍摄的照片。但是,在平板电脑启动后,我第一次打开相机时,好像捕获按钮没有响应,并且出现此错误。再次打开相机后,它又恢复了正常工作。

这是我的contentObserver:

class PictureObserver(private var appContext: WeakReference<Context>) : ContentObserver(null) {

    private val pictureSubject: PublishProcessor<Uri> = PublishProcessor.create()

    fun observe(): Flowable<Picture> =
            pictureSubject.onBackpressureBuffer()
                    .map { uri ->
                        appContext.get()?.let {
                            val cursor = it.contentResolver
                                    .query(uri,
                                            null,
                                            null,
                                            null,
                                            "date_added DESC, _id DESC")
                            if (cursor != null) {
                                if (cursor.moveToNext()) {
                                    val dataColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)
                                    val filePath = cursor.getString(dataColumn)
                                    val name = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME)
                                    return@map Picture()
                                            .apply {
                                                this.name = cursor.getString(name)
                                                this.path = filePath
                                            }
                                }
                                cursor.close()
                            }
                            return@map Picture()
                        }
                        return@map Picture()
                    }


    override fun onChange(selfChange: Boolean, uri: Uri) {
        pictureSubject.onNext(uri)
    }

}

0 个答案:

没有答案