CameraX ImageCapture 无效 URI 内容

问题描述 投票:0回答:1

我在使用 CameraX 捕获图像时遇到问题,它在图库中输出的图像已损坏,并且我在日志中收到此错误,该错误似乎指向 URI 内容的错误:

 E  Couldn't take photo:
                 androidx.camera.core.ImageCaptureException: Processing failed.
                 at androidx.camera.core.imagecapture.ProcessingNode.processInputPacket(ProcessingNode.java:180)
                 at androidx.camera.core.imagecapture.ProcessingNode.lambda$transform$0$androidx-camera-core-imagecapture-ProcessingNode(ProcessingNode.java:132)
                 at androidx.camera.core.imagecapture.ProcessingNode$$ExternalSyntheticLambda4.run(D8$$SyntheticClass:0)
                 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
                 at java.lang.Thread.run(Thread.java:923)
                 Caused by: java.lang.UnsupportedOperationException: Invalid URI content://media/external/images/media/84854
                 at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:174)
                 at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
                at android.content.ContentProviderProxy.insert(ContentProviderNative.java:549)
                 at android.content.ContentResolver.insert(ContentResolver.java:2159)
                 at android.content.ContentResolver.insert(ContentResolver.java:2121)
                at androidx.camera.core.imagecapture.JpegBytes2Disk.copyFileToMediaStore(JpegBytes2Disk.java:193)
                  at androidx.camera.core.imagecapture.JpegBytes2Disk.moveFileToTarget(JpegBytes2Disk.java:167)
               at androidx.camera.core.imagecapture.JpegBytes2Disk.apply(JpegBytes2Disk.java:70)
                at androidx.camera.core.imagecapture.JpegBytes2Disk.apply(JpegBytes2Disk.java:52)
                  at androidx.camera.core.imagecapture.ProcessingNode.processOnDiskCapture(ProcessingNode.java:198)
                  at androidx.camera.core.imagecapture.ProcessingNode.processInputPacket(ProcessingNode.java:170)
              at androidx.camera.core.imagecapture.ProcessingNode.lambda$transform$0$androidx-camera-core-imagecapture-ProcessingNode(ProcessingNode.java:132) 
             at androidx.camera.core.imagecapture.ProcessingNode$$ExternalSyntheticLambda4.run(D8$$SyntheticClass:0) 
              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
              at java.lang.Thread.run(Thread.java:923) 

这是我拍照的代码:

private fun takePhoto(specPic: String, context: Context, controller: LifecycleCameraController, onPhotoTaken: (Uri) -> Unit){
val contentResolver = context.contentResolver
val contentValues = ContentValues().apply {
    val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
    val current = LocalDateTime.now().format(formatter)
    put(MediaStore.MediaColumns.DISPLAY_NAME, specPic + "_$current")
    put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
    put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
}
val uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)

if (uri!= null) {
    // Create the output file option to store the captured image in MediaStore
    val outputFileOptions = OutputFileOptions.Builder(contentResolver, uri, contentValues).build()
    controller.takePicture(outputFileOptions, ContextCompat.getMainExecutor(context), object : ImageCapture.OnImageSavedCallback {
        override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
            val savedUri = outputFileResults.savedUri

            if (savedUri != null) {
                onPhotoTaken(savedUri)
            }
        }
        override fun onError(exception: ImageCaptureException) {
            Log.e("Camera", "Couldn't take photo:", exception)
        }

    })
}

}

android-jetpack-compose mediastore android-camerax
1个回答
0
投票

我通过改变解决了这个问题

val uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)

val uri = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
© www.soinside.com 2019 - 2024. All rights reserved.