1

I have a button which presents the choice to user whether he wants the image from gallery or camera. When user takes image from gallery I am able to successfully upload the image to firebase storage, but when user chooses camera, I am not able to upload the image to storage. Here is my onActivityResult code when user chooses camera

if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
            selectedImageUri =Uri.parse(data.getExtras().get("data").toString()) ;
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            cameraImageButton.setImageBitmap(photo);
        }

It uploads the image to my imageview, but gives me error saying

No content provider: android.graphics.Bitmap@1b9dbf5f
                                                                                 java.io.FileNotFoundException: No content provider: android.graphics.Bitmap@1b9dbf5f

When I try to upload to storage

I have gone through a similar question posted on stack overflow.

I think the accepted answer is wrong because if I use

mImageUri = data.getData();
            mSelectImage.setImageURI(mImageUri);

The above code is only right when you take image from gallery. The image is never uploaded to my imageview if I use above code and take camera image so I am not able to push it to firebase storage. Please help

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
hagrya
  • 95
  • 1
  • 13
  • Got it https://stackoverflow.com/questions/40710599/image-capture-with-camera-upload-to-firebase-uri-in-onactivityresult-is-nul – hagrya Oct 03 '17 at 16:56

1 Answers1

0

When the camera button is been clicked use the following code:

int version = Build.VERSION.SDK_INT;

        String imgFileName = "image_name".jpg";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        String pictureImagePath = storageDir.getAbsolutePath() + "/" + imgFileName;
        File file = new File(pictureImagePath);

        Uri outputFileUri;
        if(version < 24){

            outputFileUri = Uri.fromFile(file);
        } else {

            outputFileUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider",
                    file);
        }
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, 111);

In the onActivityResult method use the following code:

String imgFileName = "image_name".jpg";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        String pictureImagePath = storageDir.getAbsolutePath() + "/" + imgFileName;
        File imgFile = new  File(pictureImagePath);

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

        byte[] dataArray = bytes.toByteArray();
Seenu69
  • 1,041
  • 2
  • 15
  • 33