I am fairly new to Android and want to make an app that allows users to upload pictures using Firebase storage. After they log in, I made it so that their Uid gets sent to SharedPreferences, so that I can retrieve it in my imageupload activity later. I need it so that when the image gets sent to Firebase, it gets put under the folder of their userid.
This is my current login activity that retrieves the Uid:
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
String userId = firebaseUser.getUid();
sharedPref = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("firebasekey", userId);
editor.commit();
And this is my imageupload activity:
sharedPref = getPreferences(MODE_PRIVATE);
String UserId = sharedPref.getString("firebasekey", "1");
StorageReference storageRef= FirebaseStorage.getInstance().getReference();
StorageReference mountainsRef = storageRef.child("uploads/"+UserId+System.currentTimeMillis());
Help would be greatly appreciated.
Here you go
String userUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
UploadTask uploadTask = FirebaseStorage.getInstance().child("newFolder").getReference(userUid).putBytes(yourPhotoToByteArray);
uploadTask
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
And to retrieve image
StorageReference storageReference = FirebaseStorage.getInstance().getReference("newFolder/" + userUid);
You can read here more about FirebaseCloudStorage. Here how to upload, and here how to download them.