Home » Android » android – How to delete image from Firebase Storage?

android – How to delete image from Firebase Storage?

Posted by: admin May 14, 2020 Leave a comment

Questions:

Can you help delete an image from Firebase Storage. String deleteImage holds the full url of where the image is located in the Firebase storage.

My code is as follows, but it does not delete the image:

StorageReference deleteFile = storageReference.child(deleteImage);
            deleteFile.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Toast.makeText(EditProfile.this, "Previous Image Deleted", Toast.LENGTH_SHORT).show();
                }
            });
How to&Answers:

You need to use this method call:

StorageReference photoRef = mFirebaseStorage.getReferenceFromUrl(mImageUrl);

Then delete as you were:

photoRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
    @Override
    public void onSuccess(Void aVoid) {
        // File deleted successfully
        Log.d(TAG, "onSuccess: deleted file");
    }
    }).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Uh-oh, an error occurred!
        Log.d(TAG, "onFailure: did not delete file");
    }
});

Answer:

Use the function getReferenceFromUrl(URL)

FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
                    StorageReference storageReference = firebaseStorage.getReferenceFrenter code hereomUrl(pd.getUrl());
                    storageReference.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            Log.e("Picture","#deleted");
                        }
                    });