androidandroid-cameraandroid-camera-intentandroid-camera2

Is it possible to do this? From selecting image to capturing image


Is it possible to capture image and show it on imageview without saving on the phone? It would best if it work from API level 18. Currently my code:

private void ImageSelection()
{
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent, IMAGE_REQUEST);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == IMAGE_REQUEST && resultCode == RESULT_OK && data != null)
    {
        Uri FilePath = data.getData();
        try {

            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), FilePath);
            mImageView.setImageBitmap(bitmap);
            mImageView.setVisibility(View.VISIBLE);
            mEditText.setVisibility(View.VISIBLE);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
        if (TextUtils.isEmpty(mEditText.getText())){
            mEditText.setError("Must!");
        }

}

Solution

  • From official documentation:

    Take a photo with a camera app

    The Android way of delegating actions to other applications is to invoke an Intent that describes what you want done. This process involves three pieces:

    1. The Intent itself
    2. a call to start the external Activity
    3. some code to handle the image data when focus returns to your activity

    Here's a function that invokes an intent to capture a photo.

    static final int REQUEST_IMAGE_CAPTURE = 1;
    
    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
    

    Notice that the startActivityForResult() method is protected by a condition that calls resolveActivity(), which returns the first activity component that can handle the intent. Performing this check is important because if you call startActivityForResult() using an intent that no app can handle, your app will crash. So as long as the result is not null, it's safe to use the intent.

    Get the thumbnail

    If the simple feat of taking a photo is not the culmination of your app's ambition, then you probably want to get the image back from the camera application and do something with it.

    The Android Camera application encodes the photo in the return Intent delivered to onActivityResult() as a small Bitmap in the extras, under the key "data". The following code retrieves this image and displays it in an ImageView.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            mImageView.setImageBitmap(imageBitmap);
        }
    }