0

So I am working on the memories Firebase app and there is a stage where the user can upload photos to his memory. Each photo has uniq name "ImgLink0","ImgLink1",etc..

The function I am working on is to delete a specific photo when I am pressing a long click, but I can't reach the image in Firebase.

I tried like below but I got stuck because I can't identify the image key:

mDatabase.child(userUID).child("Memories").child(memoryName).child("Images").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot imageSnapshot : dataSnapshot.getChildren()) {
            
            String imagesKey = imageSnapshot.getKey();
            String imagesValue = (String) imageSnapshot.getValue();

            Log.e("Test","" + imagesKey + "" + imagesValue);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

And this is the data structure: enter image description here

I tried using query too but with no success because I don't have the image key.

Thank you for any help :)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mr.Goomer
  • 73
  • 7

2 Answers2

1

To remove the second URL for example, please use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference imageToDeleteRef = rootRef
    .child(uid)
    .child("Memories")
    .child("TestMem")
    .child("Images")
    .child("ImgLink1");
imageToDeleteRef.removeValue().addOnCompleteListener(/* ... */);

So I have created a reference that points exactly to ImgLink1 node and then I called .removeValue() on reference in order to delete it. So there is no need to read the value in order to perform a delete operation.

If you want to read the URL, please use these lines:

imageToDeleteRef.addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            String url1 = task.getResult().child("ImgLink1").getValue(String.class);
            Log.d("TAG", url1);
        } else {
            Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

The result in the logcat will be:

https://firebasestorage.googleapis.com...
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hey Alex,Thank you very much,that worked but I wanted to delete the value without knowing the key name. Thanks again. – Mr.Goomer Aug 13 '21 at 14:12
1

To delete a node from the database, you need to know the completely path to that node.

Assuming you do know the URL of the image to delete, but not the key (ImgLink1 or ImgLink2) that is stored under, you're going to have to use a query to look up that key.

Something like:

DatabaseReference ref = mDatabase.child(userUID).child("Memories").child(memoryName).child("Images");
Query query = ref.orderByValue().equalTo("URL of the image")
query..addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot imageSnapshot : dataSnapshot.getChildren()) {            
            imageSnapshot.getRef().removeValue();
        }
    }
    ...

Also see these related answer:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807