I'm working on an app that uploads an image to the firebase database from the gallery and camera.
From the gallery it works, but from the camera need create that image when I take it. All tutorial I found have mistakes and I don't know how do it.
private Button btnCamera, btnGallery, btnList;
private StorageReference storage; //referencia para usar Storage
private static final int GALLERY_INTENT = 1;
private static final int CAMERA_REQUEST_CODE = 2;
private static final String AUTHORITY=BuildConfig.APPLICATION_ID+".provider";
private ProgressDialog progressDialog;
private File file = null;
ImageView imgView = null;
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String fecha = df.format(c.getTime());
private EditText TextNombre;
private EditText TextApellidos;
private TextView TextSalida;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_method);
storage = FirebaseStorage.getInstance().getReference();
TextNombre = (EditText) findViewById(R.id.textN);
TextApellidos = (EditText) findViewById(R.id.textA);
TextSalida = (TextView) findViewById(R.id.salida);
progressDialog = new ProgressDialog(this);
btnCamera = (Button) findViewById(R.id.buttonCamera);
btnGallery = (Button) findViewById(R.id.buttonGallery);
btnList = (Button) findViewById(R.id.buttonList);
String dato = getIntent().getStringExtra("dato");
TextSalida.setText(dato);
This is the method to take a picture with the camera:
//-------------METODO TOMAR FOTO CON LA CAMARA-------------
btnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*Intent btnImage = new Intent(SelectMethod.this, TakePhoto.class);
startActivity(btnImage);*/
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
}
});
And this with gallery:
//-------------METODO SELECCIONAR IMAGEN DESDE GALERIA-------------
btnGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent (Intent.ACTION_PICK); //selecciona una imagen de la galería
intent.setType("image/*"); //permitimos todas las extensiones de imagenes
startActivityForResult(intent,GALLERY_INTENT);
}
});
}
OnActivityResult for two methods:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case GALLERY_INTENT:
if (requestCode == GALLERY_INTENT && resultCode == RESULT_OK) { //verificamos que la imagen se obtuvo de manera correcta
Uri uri = data.getData();
StorageReference filePath = storage.child(getIntent().getStringExtra("dato")).child(fecha + " " + uri.getLastPathSegment());
filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(SelectMethod.this, "La imagen se subió correctamente.", Toast.LENGTH_SHORT).show();
}
});
}
break;
case CAMERA_REQUEST_CODE:
if(requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
imgView.setImageBitmap(bitmap);
}
break;
}
}
}