简体   繁体   English

如何从 Firebase 存储中删除图像?

[英]How to delete image from Firebase Storage?

Can you help delete an image from Firebase Storage.你能帮助从 Firebase 存储中删除图像吗? String deleteImage holds the full url of where the image is located in the Firebase storage.字符串deleteImage包含图像在 Firebase 存储中的位置的完整 url。

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();
                }
            });

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");
    }
});

Use the function getReferenceFromUrl(URL)使用函数 getReferenceFromUrl(URL)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM