'How to create another image path using Android Studio Firebase Cloud Storage
I am currently trying to create my RegisterFarm Activity which looks like this 
My problem is that when using the upload file for both certificate and image of the farm it overwrites one another. I want to create another storage reference where it creates a certificate.jpg. I am unsure of how to add another upload.
My Current Code:
public class RegisterFarm extends AppCompatActivity {
EditText farmname, hectares;
Button upload_title, upload_pic, submit;
FirebaseFirestore fStore;
StorageReference storageReference;
FirebaseAuth fAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register_farm);
farmname = findViewById(R.id.input_farm_name);
hectares = findViewById(R.id.input_farm_hectares);
upload_title = findViewById(R.id.upload_btn1);
upload_pic = findViewById(R.id.upload_btn2);
submit = findViewById(R.id.upload_submit);
fStore = FirebaseFirestore.getInstance();
fAuth = FirebaseAuth.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Farmname = farmname.getText().toString();
String Hectares = hectares.getText().toString();
Map<String,Object> farms = new HashMap<>();
farms.put("Farm Name", Farmname);
farms.put("Hectares", Hectares);
fStore.collection("Farms")
.add(farms)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(RegisterFarm.this, "Farm Registered.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(RegisterFarm.this, "Upload Error", Toast.LENGTH_SHORT).show();
}
});
}
});
upload_title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//open gallery
Intent opengallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(opengallery,1000);
}
});
upload_pic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//open gallery
Intent opengallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(opengallery,1000);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @androidx.annotation.Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1000){
if(resultCode == Activity.RESULT_OK){
Uri imageUri = data.getData();
//profilepic.setImageURI(imageUri);
uploadImagetoFirebase(imageUri);
}
}
}
private void uploadImagetoFirebase(Uri imageUri) {
//upload Image to Firebase
StorageReference fileRef = storageReference.child("Farms/" +fAuth.getCurrentUser().getUid()+"/farm.jpg");
fileRef.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
fileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Toast.makeText(RegisterFarm.this, "Image Uploaded Successfully", Toast.LENGTH_SHORT).show();
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(RegisterFarm.this, "Image Upload Failed", Toast.LENGTH_SHORT).show();
}
});
}
Solution 1:[1]
You're uploading the image to this location on Cloud Storage:
storageReference.child("Farms/" +fAuth.getCurrentUser().getUid()+"/farm.jpg");
Since you always name the file farm.jpg, you just end up with the last image that you uploaded for that user.
If you want to store multiple images for a user, you'll have to generate some unique path for each file they upload. For example, you could add the current timestamp to the file name:
storageReference.child("Farms/" +fAuth.getCurrentUser().getUid()+"/farm_"+System.currentTimeMillis()+".jpg");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Frank van Puffelen |
