Problem converting an image to URI to BITMAP when uploading to Firebase

0

The problem I have is when uploading some images through chat, but sometimes these can be very heavy and it would not be convenient to upload such heavy images to Firebase . so choose to take the image of Uri that brings the ActivityForResult and convert it to Bitmap , but when reviewing in the console Firebase it turns out that the image did not lose size but rather before increase.

Here the code:

byte[] data_byte = null;
if (requestCode == PHOTO_SEND && resultCode == getActivity().RESULT_OK){
    Uri u = data.getData();
    String id_grupo = getPreferences("id_grupo");
    try {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), u);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        data_byte = bytes.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    }
    storageReference = storage.getReference("imagenes_chat_"+id_grupo);
    final StorageReference fotoReferencia = storageReference.child(u.getLastPathSegment());
    uploadTask = fotoReferencia.putBytes(data_byte);
    uploadTask.addOnSuccessListener(getActivity(), new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            db = new SQLiteHandler(getActivity());
            HashMap<String, String> user = db.getUserDetails();
            String name = user.get("name");
            @SuppressWarnings("VisibleForTests") Uri u = taskSnapshot.getDownloadUrl();
            MensajeEnviar m = new MensajeEnviar("Ha enviado una foto",u.toString(),name,"","2",ServerValue.TIMESTAMP);
            databaseReference.push().setValue(m);
        }
    });
    
asked by Rosyec Parrado 16.07.2018 в 17:26
source

1 answer

0

What happens is that you are passing the value wrong to the compress, as it says in the documentation link

public boolean compress (Bitmap.CompressFormat formato, 
                int calidad, 
                OutputStream stream)
  

int: Compressor suggestion, 0-100. 0 means compression for   small size, 100 which means compression for a quality   maximum. Some formats, such as PNG without loss, will ignore the   Quality configuration

try to reduce the 100 to 50 or 40 and you will see that the image is smaller

0 - Maximum compression (smaller size)

100 - Little compression for more quality (larger size)

Change this line

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

for this

bitmap.compress(Bitmap.CompressFormat.JPEG, 20, bytes);

There you should notice a major change in the size of the image, but remember that each time you compress more, but lose quality, that's where you should play with those values to see what is your best quality / size for firebase

    
answered by 17.07.2018 / 00:07
source