I need to have my App open the camera, take a picture and go back to the previous Fragment and click the Upload button, send that photo to the Firebase Storage .
I was able to do with images that are already saved in the phone, but with the camera not working, 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);
}
onStartResultActivity method :
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 writes Images 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);
}
Image upload procedure:
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();
}
}