1
I need to make mine App open the camera, take a photo and when returning to the Fragment previous and click on the "Upload" button, send this photo to the Firebase Storage.
I managed to do with images that are already saved on the phone, but with the camera does not work, the following error is displayed:
An unknownerror occurred, Please check the HTTP result code and Inner Exception for server Response
But the shipping procedure is the same!
Procedure to open the camera on onClick:
public void abrirCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, PICK_IMAGE_CAMERA);
}
Method onStartResultActivity:
case PICK_IMAGE_CAMERA:
if (resultCode == RESULT_OK && data != null) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
filePath = escreveImagens(bitmap);
foto.setImageBitmap(bitmap);
}
break;
Function scribes used above:
public Uri escreveImagens(Bitmap bmp) {
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bytes = stream.toByteArray();
FileOutputStream pos = new FileOutputStream(nomeArquivo);
pos.write(bytes);
pos.close();
} catch (Exception e) {
e.printStackTrace();
}
return Uri.parse(nomeArquivo);
}
Procedure for uploading images:
private void uploadImagem() {
if (filePath != null) {
final ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.setTitle("Enviando...");
progressDialog.show();
StorageReference reference = storageReference.child("Fotos/" + alunoLogado.getMatricula());
reference.putFile(filePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(getContext(), "Foto enviada!", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
progressDialog.setMessage((((int) progress) + "% enviados..."));
}
});
} else {
Toast.makeText(getContext(), "Ocorreu um erro. Tente novamente!", Toast.LENGTH_SHORT).show();
}
}