Home > OS >  Identify the selected option from Intent.ACTION_CHOOSER from the registerForActivityResult
Identify the selected option from Intent.ACTION_CHOOSER from the registerForActivityResult

Time:11-26

I am using an action chooser intent to ask the user to choose one of the following from a fragment:

MediaStore.ACTION_IMAGE_CAPTURE
MediaStore.ACTION_VIDEO_CAPTURE
Intent.ACTION_GET_CONTENT

Android Action Chooser

I want to be able to distinguish the selected action of the user because I have different functions per action.

Below is my current code.

private val intentLauncher = registerForActivityResult(   
    ActivityResultContracts.StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        //Identify the intent selected

        //TODO: image from camera
        //TODO: video from camera
        //TODO: any file
    }
}



private fun dispatchActionChooserIntent() {
    Intent(Intent.ACTION_CHOOSER).also { actionChooserIntent ->

        val cameraIntent = createCameraIntent(MediaStore.ACTION_IMAGE_CAPTURE)
        val videoIntent = createCameraIntent(MediaStore.ACTION_VIDEO_CAPTURE)
        val filePickerIntent = createFilePickerIntent()

        actionChooserIntent.putExtra(Intent.EXTRA_INTENT, filePickerIntent);
        actionChooserIntent.putExtra(
            Intent.EXTRA_INITIAL_INTENTS,
            arrayOf<Intent>(cameraIntent, videoIntent)
        );
        cameraIntent.putExtra("intentAction",Intent.ACTION_CHOOSER)
        actionChooserIntent.putExtra(Intent.EXTRA_TITLE, "")
    }
}



private fun createFilePickerIntent(fileType: String = "*/*"): Intent {
    return Intent(Intent.ACTION_GET_CONTENT).also { filePickerIntent ->
        filePickerIntent.type = fileType
        filePickerIntent.addCategory(Intent.CATEGORY_OPENABLE)
        filePickerIntent.resolveActivity(
            (activity as AppCompatActivity).applicationContext.packageManager)
    }
}



private fun createCameraIntent(cameraAction: String): Intent {
    return Intent(cameraAction).also { cameraIntent ->
        // Ensure that there's a camera activity to handle the intent
        cameraIntent.resolveActivity(
            (activity as AppCompatActivity).applicationContext.packageManager)
        

        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraIntentURI)

    }
}
the result only includes the resultCode and the data

Sample result from taking a photoenter image description here

Sample result from taking a video enter image description here

Sample result from choosing a file enter image description here

CodePudding user response:

If the received uri is null the camera was choosen.

The MediaStore.ACTION_VIDEO_CAPTURE honors Intent.EXTRA_OUTPUT and you will use your fileprovider there.

In contrary to the camera action you will get the supplied uri back. (Google has learned something). So if you receive an uri in onActivityResult you can check for uri.getAuthority().

if the authority is the same as your fileprovider you know it is from video capture.

CodePudding user response:

There can be two solutions.

  1. Android way When you receive the URI as data as a result. Process that URI and try to get the mime type. Based on mime type you can understand what type of data you have received. Check how it can be done here https://developer.android.com/training/secure-file-sharing/retrieve-info

  2. Logical way Convert the Uri into an instance of type File. Get the extension of a file and check what type of extension it is. jpeg, png, etc fall into the image category, mp4 kind of extension is a video and the rest can be any other file. Try this to get the file extension https://www.tutorialkart.com/kotlin/kotlin-get-file-extension/#:~:text=Get File Extension in Kotlin,extension is a String value.

I would recommend exploring option 1. That's the right and safe approach.

Good luck!

  • Related