结果代码 = 0 extra_output 在 android 8 上的相机意图

resultcode = 0 camera intent with extra_output on android 8

我正在尝试使用我的应用程序使用相机意图和 EXTRA_OUTPUT 拍照。这适用于 android 10 和 9(虽然我对 10 使用稍微不同的方法),但是当我尝试 运行 它时 android 8 或 7 onActivityResult 得到 resultCode 0 . 我正在使用 android 开发者页面上提供的示例拍照 (link)

private fun dispatchTakePictureIntent() {
    Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
        // Ensure that there's a camera activity to handle the intent
        takePictureIntent.resolveActivity(packageManager)?.also {
            // Create the File where the photo should go
            val photoFile: File? = try {
                createImageFile()
            } catch (ex: IOException) {
                null
            }

            // Continue only if the File was successfully created
            photoFile?.also {
                imageUri = FileProvider.getUriForFile(
                    applicationContext,
                    "com.example.gpscamera.fileprovider",
                    it
                )
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO)
            }
        }
    }
}

private fun createImageFile(): File {
    // Create an image file name
    val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
    imageName = "IMG$timeStamp"
    val storageDir: File? = File(getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/GPSCamera")
    //Log.d("path",storageDir.toString())
    return File(
        storageDir,
        "$imageName.jpg"
    ).apply {
        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = absolutePath
    }
}

系统确实抛出错误:

2020-05-06 17:05:05.584 6619-6619/? E/CAM_StateSavePic: exception while saving result to URI: Optional.of(content://com.example.gpscamera.fileprovider/GPSCamera/GPSCamera/IMG20200506_170337.jpg)
java.io.FileNotFoundException: No such file or directory
    at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:144)
    at android.content.ContentProviderProxy.openAssetFile(ContentProviderNative.java:621)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1000)
    at android.content.ContentResolver.openOutputStream(ContentResolver.java:742)
    at android.content.ContentResolver.openOutputStream(ContentResolver.java:718)
    at com.android.camera.captureintent.state.StateSavingPicture.onEnter(StateSavingPicture.java:84)
    at com.android.camera.captureintent.stateful.StateMachineImpl.jumpToState(StateMachineImpl.java:62)
    at com.android.camera.captureintent.stateful.StateMachineImpl.processEvent(StateMachineImpl.java:110)
    at com.android.camera.captureintent.state.StateOpeningCamera.onClick(StateOpeningCamera.java:307)
    at android.view.View.performClick(View.java:5610)
    at android.view.View$PerformClick.run(View.java:22265)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

虽然这再次发生在 android 9(使用相同代码的地方)。我缺少什么导致它无法在 android 7 和 8 上工作?

原来这个错误是因为我试图放置文件的文件夹还不存在。可以在下面找到改进的代码:

private fun createImageFile(): File {
    // Create an image file name
    val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
    imageName = "IMG$timeStamp"
    val storageDir: File? = File(getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/GPSCamera")
    //Log.d("path",storageDir.toString())
    return File(
        storageDir,
        "$imageName.jpg"
    ).apply {
        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = absolutePath
        // Check if parent folder exists, if not create the folder
        if (!parentFile.exists()){
            parentFile.mkdirs()
        }
    }
}