Search code examples
androidkotlinandroid-8.0-oreoandroid-camera-intentandroid-9.0-pie

resultcode = 0 camera intent with extra_output on android 8


I am trying to take a picture with my app using the camera intent and EXTRA_OUTPUT. This works fine for android 10 and 9 (though I use a slightly different approach for 10), however when I try to run it on android 8 or 7 the onActivityResult get a resultCode 0. I am using the example provided on the android developers page Take photos (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
    }
}

The system does throw an error:

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$9.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)

Though once again, this does not happen on android 9 (where the same code is used). What am I missing that causes it to not work on android 7 and 8?


Solution

  • turns out that the error was caused by the fact that the folder I tried to place the file in did not yet exist. Improved code can be found below:

    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()
            }
        }
    }