I need to find out where the error is in my code below where the idea is if I select a photo, I send this photo to Firebase Storage and soon after I have to retrieve the link to download the photo to me save in the image field of the client class.
The problem that is happening is that even having the photo onSuccess(UploadTask.TaskSnapshot taskSnapshot)
is not working
public class ClientescadActivity extends AppCompatActivity {
private Toolbar toolbarcadcli;
private TextInputEditText edtNomeCli;
private TextInputEditText edtTelefoneCli;
private Clientes clientes;
private ImageView imgCli;
private Uri filepath;
private final int REQUEST_CODE = 1234;
public static final String FB_STORAGE_PATH = "image/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_clientescad );
toolbarcadcli = findViewById( R.id.toolbarcadcli );
setSupportActionBar( toolbarcadcli );
edtNomeCli = findViewById( R.id.edtNomeCli );
edtTelefoneCli = findViewById( R.id.edtTelefoneCli );
edtTelefoneCli.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
imgCli = findViewById(R.id.imgCli);
imgCli.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ChooseImage();
}
});
RealTime.InicializaFirebase (getApplicationContext(), "Clientes");
}
private void ChooseImage()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Selecione uma foto"),REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null && data.getData() != null)
{
filepath = data.getData();
try
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),filepath) ;
imgCli.setImageBitmap(bitmap);
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.menu_clientes_cad,menu );
return super.onCreateOptionsMenu( menu );
}
private boolean ValidaCampos()
{
boolean res;
String nome = edtNomeCli.getText().toString().trim();
String phone = edtTelefoneCli.getText().toString().trim();
clientes.setNome(nome);
clientes.setTelefone(phone);
if (res = IsFieldEmpty( nome ))
{
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle(R.string.title_atencao);
dlg.setMessage(R.string.message_cliente_obrigatorio);
dlg.setNeutralButton("Ok",null);
dlg.show();
edtNomeCli.requestFocus();
}
return res;
}
private void SalvarDados()
{
clientes = new Clientes();
if (ValidaCampos() == false)
{
try
{
StorageReference ref;
clientes.imagem = "https://firebasestorage.googleapis.com/v0/b/salaobeleza-29ca9.appspot.com/o/image%2Fphotoprofile.png?alt=media&token=ff42f8a8-1a21-43c1-9251-6b227a99c0cd";
if(filepath != null)
{
ref = RealTime.storageReference.child(FB_STORAGE_PATH + System.currentTimeMillis() + "." + getImageExt(filepath));
ref.putFile(filepath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
clientes.imagem = taskSnapshot.getDownloadUrl().toString();
}
});
}
String mId = RealTime.databaseReference.push().getKey();
RealTime.databaseReference.child(mId).setValue(clientes);
Toast.makeText(ClientescadActivity.this, R.string.message_cadasto_sucesso, Toast.LENGTH_SHORT).show();
finish();
}catch (Exception ex){
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle(R.string.title_atencao);
dlg.setMessage(ex.getMessage());
dlg.setNeutralButton("Ok",null);
dlg.show();
}
}
}
private boolean IsFieldEmpty(String valor)
{
boolean resultado = (TextUtils.isEmpty( valor ) || valor.trim().isEmpty());
return resultado;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.action_cli_cad_ok:
SalvarDados();
break;
case R.id.action_cli_cad_remove:
Toast.makeText( this, "Removendo...", Toast.LENGTH_SHORT ).show();
break;
}
return super.onOptionsItemSelected( item );
}
public String getImageExt(Uri uri)
{
ContentResolver contentResolver = getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
}
}