Number70
Number70

Reputation: 453

onActivityResult is not displaying image

So, I want to take a picture and display it on the screen. I can press the button and take a picture but it isn't showing it on the ImageView. I can't find the solution with searching onActivityresult.

What I have now:

 public class MainActivity extends AppCompatActivity {


    ImageView imageView;

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

        Button btnCamera = findViewById(R.id.btnCamera);
        imageView = findViewById(R.id.imageView);


        // Open camera and take picture
        btnCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 0);

            }
        });

    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode,Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Bitmap bitmap = (Bitmap)data.getExtras().get("Data");
        imageView.setImageBitmap(bitmap);
    }
}

Upvotes: 0

Views: 481

Answers (2)

Ahmed El-Nakib
Ahmed El-Nakib

Reputation: 179

try this

if (requestCode == 0 && resultCode == RESULT_OK )
    {
        Uri imgUri = data.getData();
        try {
            Bitmap bm = MediaStore.Images.Media.getBitmap(getContentResolver(), imgUri);
            imageView.setImageBitmap(bm);}}

Upvotes: 0

Dinesh Shingadiya
Dinesh Shingadiya

Reputation: 1008

Use this:

Bitmap bitmap = (Bitmap)data.getExtras().get("data");

Instead of:

Bitmap bitmap = (Bitmap)data.getExtras().get("Data");

Upvotes: 6

Related Questions