Picture coming from camera or gallery?

后端 未结 2 757
情书的邮戳
情书的邮戳 2020-12-29 08:17

I have an intent chooser that allows me to pick image from gallery or camera like this:

    Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT,null)         


        
2条回答
  •  Happy的楠姐
    2020-12-29 09:06

    You can distinguish by using REQUEST_CODE

        private static final int PICK_FROM_CAMERA = 1;
            private static final int PICK_FROM_GALLARY = 2;
    
            /* For Image capture from camera */
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, PICK_FROM_CAMERA);   
    
        /* For Image capture from Gallary*/             
            startActivityForResult(new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI),
                                            PICK_FROM_GALLARY);
                            }
    
    
    
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
            super.onActivityResult(requestCode, resultCode, intent);
    
            switch (requestCode) {
                   case PICK_FROM_CAMERA:
                if (resultCode == Activity.RESULT_OK) {
                 Bitmap bitmapImage = (Bitmap) intent.getExtras().get("data");
                  }
                  break;
    
                    case PICK_FROM_GALLARY:
                   if (resultCode == Activity.RESULT_OK) {
                  Uri mImageCaptureUri = intent.getData();
                   }
                 break;
             }
    }
    

提交回复
热议问题