I'm trying to make a social media application with firebase in android studio, now my application is finished, it works smoothly, but there is a problem, there is a delete button on the shared post, when this button is clicked, I want it to delete only that selected picture.
Can you help me, please?
if you're asking about firebase database you can do something like
// Reference to the node you want to delete
DatabaseReference postRef = FirebaseDatabase.getInstance().getReference("posts").child(postId);
// Delete the node
postRef.removeValue();
in case you are talking about firebase storage you can do something like
// Reference to the image you want to delete
StorageReference imageRef = storageRef.child("images/myimage.jpg");
// Delete the image
imageRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
// File deleted successfully
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Uh-oh, an error occurred!
}
});
you must include the dependency in build.gradle
implementation 'com.google.firebase:firebase-storage:19.2.0'
sorry i didn't had enough reputation here to comment and ask so directly posting an answer
Related
my image is stored inside firebase storage and its url is stored inside the realtime database. Firebase realtime and storage are both like this:
Type1
----Time1
--------elements of type1
----Time2
--------elements of type2
Type2
....
I let the users change element's time so I have to move an element from a branch to another.
My strategy is to create a new element on the new branch and then remove the old one.
The problem is in the creation of the new element in the chosen branch, specifically I have problems setting the image of the new element equal to that of the old one.
I tried to use Uri.parse() passing the url saved as string in the realtime database and I also tried to use storagereference.getdownloadurl, both I got this error:
could not locate file for uploading:https://firebasestorage.googleapis.com/v0/b/re...
E/StorageException: StorageException has occurred.
An unknown error occurred, please check the HTTP result code and inner exception for server response.
Code: -13000 HttpResult: 0
And it's not sense because If I click that link in the error, it shows me the correct image, so why it can't locate it?!
How can I do it?
storageReference = FirebaseStorage.getInstance().getReference().child("images/Anime/General/Test1");
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
storageReference = FirebaseStorage.getInstance().getReference().child("images/Anime/Future/Test1");
storageReference.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
...
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
}
});
}
});
The error is on this line:
storageReference.putFile(uri)...
You have to download the bytes from firebase like this:
storageReference = FirebaseStorage.getInstance().getReference().child("images/Anime/General/Test1");
storageReference.getBytes(1024*1024).addOnSuccessListener(new OnSuccessListener<byte[]>() {
#Override
public void onSuccess(byte[] bytes) {
//here add new element in the server with storageReference.putbytes(bytes)..
}
});
So I have a collection in Cloud Firestore which I populate by clicking some buttons in my application. Once I close the app, I want the collection to be empty. Is there any way I can do that? I tried to implement onStop() method, but it does nothing.
protected void onStop() {
super.onStop();
db.collection("AddPlaces").document()
.delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.d("SUCCES", "DocumentSnapshot successfully deleted!");
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.w("FAILURE", "Error deleting document", e);
}
});
}
Any ideas if I can achieve that?
An empty collection means a collection with no documents. If you want to delete all documents in a collection, I recommend you see my answer in the following post:
How delete a collection or subcollection from Firestore?
Why your code is not working? Is because of the following line of code:
db.collection("AddPlaces").document()
Which basically generates a document with a random ID, and nothing more. So when you call delete(), you're trying to delete a single document and not all documents in the collection, hence that behavior.
One more thing to note is that if you delete all documents in the collection, that collection will not exist anymore. It will exist again when you'll write a new document in it.
This question already has answers here:
FirebaseStorage: How to Delete Directory
(15 answers)
Closed 3 years ago.
I am using the firebase realtime database. User want to delete his Complete data from Firebase of particular id.Data from Authentication, Database is deleted. But not able to delete his images complete folder from Firebase storage. it can deleted by only particular file name not exactly the complete folder.
private void deleteUserStorage(){
StorageReference storageReference;
storageReference = FirebaseStorage.getInstance().getReference().child(mUid);
storageReference.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(UserDetails.this, "got success", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.e("not",""+e);
}
});
}
com.google.firebase.storage.StorageException: Object does not exist at location
I think you have to delete the files one by one. In firebase Documentation, you can check only gave the instruction to delete only one file not given any instructions to delete all the folder.
I was creating an app and connect it to firebase and I have successfully uploaded the data by firebase documentation, and now I can't get it, I couldn't find any documentation about it.
Can you please show me the code of how to get my data AND set it to my model like for example:
Model.setTitle(firebase.collection.get("Title"));
Get your database and then use a ValueEventListener :)
ValueEventListener postListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
Post post = dataSnapshot.getValue(Post.class);
HERE GET THE VARIABLE AND SET TO YOUR TITLE :)
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// ...
}
};
mPostReference.addValueEventListener(postListener);
In some cases you may want a callback to be called once and then immediately removed, such as when initializing a UI element that you don't expect to change. You can use the addListenerForSingleValueEvent() method to simplify this scenario: it triggers once and then does not trigger again.
https://firebase.google.com/docs/database/android/read-and-write
I am fetching data on Firebase from time to time because I am tracking someone's GPS. That someone is saving his location in an interval of 5 minutes so that I can track his GPS. But my problem is how do I fetch data from Firebase with an interval of 5 minutes too?
Or is there any possible way other than this?
Thanks in advance. :)
So if someone is updating his/her location in every five minutes, then you really don't have to run a CounterDownTimer in your side to track the location in every five minutes. So I think you just need to add a listener to that node at Firebase that you want to track.
So here's a simple implementation for you. Copied from Firebase Tutorial
Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts");
// Attach an listener to read the data at our posts reference
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
System.out.println(snapshot.getValue());
}
#Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
I'm copying quotes from there too. So that you know it serves your purpose.
This method will be called anytime new data is added to our Firebase
reference, and we don't need to write any extra code to make this
happen.
So each time the person you want to track will update his/her location, you'll get a callback in the method stated above and will take necessary action. You really don't have to implement a polling mechanism to do the tracking. That's the way Firebase works actually.
No Needs to put any kind of service of Scheduler to retrieve data from firebase.
As Firebase provide realtime database .. whenever you push your data on Firebase Database the listener will trigger and you can retrieve your data..
Implement following Listener, using this you can retrieve your data whenever database get update.
DatabaseReference mDatabase =FirebaseDatabase.getInstance().getReference();
ValueEventListener yourModelListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get YOURMODEL object and use the values to update the UI
YourModel mModel = dataSnapshot.getValue(YourModel.class);
Log.e("Data : ",""+mModel);
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
};
mDatabase.addValueEventListener(yourModelListener);
For More Info about Listeners .. https://firebase.google.com/docs/database/android/retrieve-data