Android How can I call Camera or Gallery Intent Together

后端 未结 7 1779
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 09:27

If I want to capture image from native camera, I can do:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTR         


        
7条回答
  •  Happy的楠姐
    2020-12-02 10:01

    Let's say that you have two intents. One that would open a camera, one that would open a gallery. I will call these cameraIntent and gallerIntent in the example code. You can use intent chooser to combine these two:

    Kotlin:

    val chooser = Intent.createChooser(galleryIntent, "Some text here")
    chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(cameraIntent))
    startActivityForResult(chooser, requestCode)
    

    Java:

    Intent chooser = Intent.createChooser(galleryIntent, "Some text here");
    chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { cameraIntent });
    startActivityForResult(chooser, requestCode);
    

    This is, as you asked, how you can put the two together (without having to make your own UI/ dialog).

提交回复
热议问题