Reputation: 21
I have 2 methods, the first one stores for cloud storage the other method(Savingtofirestore) saves Item data to firestore and downloads image url .I have a collection called Images and within it i have a document that has an item of the authenticated user .
private void uploadImageToStorage() {
if (filepath != null) {
// Code for showing progressDialog while uploading
ProgressDialog progressDialog
= new ProgressDialog(AddItem.this);
progressDialog.setTitle("Uploading...");
progressDialog.show();
// Defining the child of storageReference
StorageReference ref
= storageReference
.child(
"images/"
+ UUID.randomUUID().toString());
// adding listeners on upload
// or failure of image
ref.putFile(filepath)
.addOnSuccessListener(
new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(
UploadTask.TaskSnapshot taskSnapshot)
{
Task<Uri> uriTask = taskSnapshot.getStorage().getDownloadUrl();
while(!uriTask.isSuccessful());
downloadurl = uriTask.getResult().toString();
if(uriTask.isSuccessful())
{
SavingToFirestore(downloadurl);
}
// Image uploaded successfully
// Dismiss dialog
progressDialog.dismiss();
/*Toast
.makeText(AddItem.this,
"Image Uploaded!!",
Toast.LENGTH_SHORT)
.show();
*/
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e)
{
// Error, Image not uploaded
progressDialog.dismiss();
Toast
.makeText(AddItem.this,
"Failed " + e.getMessage(),
Toast.LENGTH_SHORT)
.show();
}
})
.addOnProgressListener(
new OnProgressListener<UploadTask.TaskSnapshot>() {
// Progress Listener for loading
// percentage on the dialog box
@Override
public void onProgress(
UploadTask.TaskSnapshot taskSnapshot)
{
double progress
= (100.0
* taskSnapshot.getBytesTransferred()
/ taskSnapshot.getTotalByteCount());
progressDialog.setMessage(
"Uploaded "
+ (int)progress + "%");
}
});
}
}
private void SavingToFirestore(String url){
// String id = f.getCurrentUser().getUid();
firebaseFirestore.collection("users").document(user_id).get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
Map<String,String> userMap = new HashMap<>();
userMap.put("name",name);
userMap.put("price",price);
userMap.put("description",description);
userMap.put("image",downloadurl.toString());
firebaseFirestore.collection("images").document(user_id).set(userMap)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) { //if the data is saved successfully return to the main activity
itemName.setText("");
itemPrice.setText("");
itemDescription.setText("");
itemImage.setImageResource(R.drawable.itemphoto);
Toast.makeText(AddItem.this,"Item Upload successfully😃🎉",Toast.LENGTH_LONG).show();
Intent intent = new Intent(AddItem.this,MainActivity.class);
startActivity(intent);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(AddItem.this,"Error : "+e.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
});
}
I am so lost now because i dont not know what am doing wrong ? Maybe i should loop the process?
Upvotes: 1
Views: 56
Reputation: 1413
As I understand, user is adding items the previously added one is replaced with the new one, It happens because you are using userID as document ID so you need to create separate document every time.
You can try something like this-
Map<String,String> userMap = new HashMap<>();
userMap.put("userId",user_id);
userMap.put("name",name);
userMap.put("price",price);
userMap.put("description",description);
userMap.put("image",downloadurl.toString());
firebaseFirestore.collection("images").add(userMap)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(Void aVoid) { //if the data is saved successfully return to the main activity
itemName.setText("");
itemPrice.setText("");
itemDescription.setText("");
itemImage.setImageResource(R.drawable.itemphoto);
Toast.makeText(AddItem.this,"Item Upload successfully😃🎉",Toast.LENGTH_LONG).show();
Intent intent = new Intent(AddItem.this,MainActivity.class);
startActivity(intent);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(AddItem.this,"Error : "+e.getMessage(),Toast.LENGTH_SHORT).show();
}
});
Upvotes: 1