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);
}
});