Upload image to Firebase using the camera phone

0

I have an onActivityResult, with two Case, one to choose an image from the Gallery, show it in the activity and then upload it to Firebase, all right here, then the other Case is to be able to take a photo with the camera and the sample, I tried to insert the code for tmb to upload that photo from the camera to FireBase but it does not work. Is it different from what I have in the case of the Gallery?

Code onActivityResult

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

    if(resultCode == RESULT_OK){
        switch (requestCode){
            case PHOTO_CODE:
                MediaScannerConnection.scanFile(this, new String[] { mPath }, null, new MediaScannerConnection.OnScanCompletedListener() {
                    @Override
                    public void onScanCompleted(String path, Uri uri) {
                        Log.i("ExternalStorage", "Scanned" + path + ":");
                        Log.i("ExternalStorage", "-> Uri = " + uri);
                    }
                });
                imageBitmap = BitmapFactory.decodeFile(mPath);
                foto.setImageBitmap(imageBitmap);
                break;
            case SELECT_PICTURE:
                Uri path2 = data.getData();
                //Codigo para subir la imagen a Firebase
                StorageReference filePath2 = mStorage.child("Fotos Aviso").child(path2.getLastPathSegment());
                filePath2.putFile(path2).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        Toast.makeText(Fotos.this, "Se ha subido la foto a FireBase", Toast.LENGTH_SHORT).show();
                    }
                });
                foto.setImageURI(path2);
                imageBitmap = drawableToBitmap(foto.getDrawable());
                break;
        }
    }
}
    
asked by Cristian Prieto Beltran 15.05.2017 в 10:15
source

1 answer

1

I have a similar personal project and the photos I take with the camera I upload with these methods. I hope they serve you.

@Override
    public void uploadPhotoToDeal(Bitmap photo, String dealId) {

        final String idImage = dealId.concat(".jpg");

        storageReference = FirebaseHelper.getStorageReference().child("image/" + idImage);

        final byte[] data = getImageCompressed(photo);

        UploadTask uploadTask = storageReference.putBytes(data);

        uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                HashMap<String, Object> uriChildren = new HashMap<String, Object>();
                uriChildren.put("photo", idImage);

                myDealReference.updateChildren(uriChildren);

                post(DealDetailsEvent.onImageAdded, null, null);
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

            }
        });
    }

    private byte[] getImageCompressed(Bitmap photo) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        return baos.toByteArray();
    }

I also leave the link to the project in case you need to look at something else.

link

    
answered by 15.05.2017 в 11:40