Xóa file bằng Cloud Storage trên Android

Sau khi tải file lên Cloud Storage, bạn cũng có thể xóa chúng.

Xóa một tập tin

Để xóa một tập tin, trước tiên hãy tạo một tài liệu tham khảo . vào tập tin đó. Sau đó gọi phương thức delete() trên tham chiếu đó.

Kotlin+KTX

// Create a storage reference from our app
val storageRef = storage.reference

// Create a reference to the file to delete
val desertRef = storageRef.child("images/desert.jpg")

// Delete the file
desertRef.delete().addOnSuccessListener {
    // File deleted successfully
}.addOnFailureListener {
    // Uh-oh, an error occurred!
}

Java

// Create a storage reference from our app
StorageReference storageRef = storage.getReference();

// Create a reference to the file to delete
StorageReference desertRef = storageRef.child("images/desert.jpg");

// Delete the file
desertRef.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!
    }
});

Xử lý lỗi

Có một số lý do khiến lỗi có thể xảy ra khi xóa tệp, bao gồm cả tệp không tồn tại hoặc người dùng không có quyền xóa tệp mong muốn. Bạn có thể tìm thêm thông tin về lỗi trong phần Xử lý lỗi của tài liệu.