Java – android studio cannot display images in ImageView

android studio cannot display images in ImageView… here is a solution to the problem.

android studio cannot display images in ImageView

Global variables

static final int CAMERA_REQUEST = 1;
ImageView imageview;
Bitmap bitmap;

When created:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

References

imageview = findViewById(R.id.image_view);

onclick listener and intent:

Button take_photo_btn = findViewById(R.id.photo_btn);
take_photo_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAMERA_REQUEST);
    }
});

This is the result of the activity:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

if (requestCode == CAMERA_REQUEST && requestCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        bitmap = (Bitmap) extras.get("image");
        imageview.setImageBitmap(bitmap);
    }
}

After taking a photo, I don’t see it in ImageView, what is the problem?

Solution

If you are using Marshmallow, you must implement runtime permissions for the camera used in Android.

For more details, visit this link https://developer.android.com/training/permissions/requesting

Related Problems and Solutions