0

I am trying to take a picture with the camera of "Nexus 5X API 26" and show it in ImageView field, before uploading it to Firebase.

First problem is that after taking a picture, it does not show up in imageView. I am doing the following:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, CAMERA_REQUEST_CODE);}

And then I was trying to show the picture in the same way I do for the pictures taken from Gallery:

filePath = data.getData();
   try {
      Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
      imageView.setImageBitmap(bitmap);
   } catch (IOException e) {
       e.printStackTrace();
   }

The part which I do not understand is how data.getData() works in both cases (for Gallery and for Camera)?

I guess that the uploadImage() method, should be the same for both Gallery and Camera uploads (it works for Gallery already so...).

So the thing that I am currently missing is that I am not getting the filePath, I guess? Is it necessary to "temporarily save" the camera's picture in order to .getData()? Or it can work without any kind of "saving"?

I just want the user to take a picture and it should be sent to Firebase. Not necessary for user to see it in imageView first, just to get the uri (data) in order to upload it.

view when i open the camera view after taking a picture

  • There's probably an Exception being thrown. Check what your catch block prints. – TheWanderer Nov 09 '18 at 15:21
  • `getData()` works entirely differently for camera intent. Check https://stackoverflow.com/a/40715056/192373 and links around that. Usually, as in https://stackoverflow.com/questions/45046001/how-to-configure-action-image-capture-to-store-the-photo-in-public-external-stor, we pass EXTRA_OUTPUT to tell the camera app where to store the picture. Instead, you can [get the last picture taken by camera](https://stackoverflow.com/a/8557147/192373) in **onActivityResult()**. – Alex Cohn Nov 10 '18 at 06:38
  • Thanks, the posts + further refinement worked out in the end :) – Marko Otasevic Nov 23 '18 at 13:06

1 Answers1

0

This will help you.

Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       startActivityForResult(intent,7);


     @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

            super.onActivityResult(requestCode, resultCode, data);


                    bitmap= (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream baos=new ByteArrayOutputStream();

            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] b = baos.toByteArray();
            imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);

            byte[] imageAsBytes = Base64.decode(imageEncoded.getBytes(), Base64.DEFAULT);
            InputStream is=new ByteArrayInputStream(imageAsBytes);
            bitmap1=BitmapFactory.decodeStream(is);
            img.setImageBitmap(bitmap1);


        }
Vishal Sharma
  • 1,051
  • 2
  • 8
  • 15