使用URL从Firebase存储中删除文件

时间:2017-07-14 12:31:33

标签: android firebase firebase-storage

您好我正在尝试使用文件网址从Firebase存储中删除文件。 我的问题是无法解析getReferenceFromUrl()。

示例代码:

 StorageReference mStorageRef;
    String storageurl = "http:sample"
    mStorageRef = FirebaseStorage.getInstance().getReference();
        StorageReference ref2 = mStorageRef.getReferenceFromUrl(storageurl);
        ref2.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                // File deleted successfully
                Toast.makeText(getContext(), "file deleted", Toast.LENGTH_SHORT).show();
                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");
            }
        });

任何帮助都将不胜感激。

5 个答案:

答案 0 :(得分:5)

 StorageReference storageReference = FirebaseStorage.getInstance().getReferenceFromUrl("https://firebasestorage.googleapis.com/v0/b/***********************-5fac-45b6-bbda-ed4e8a3a62ab");
 storageReference.delete().addOnSuccessListener(new OnSuccessListener<Void>() { 
    @Override 
    public void onSuccess(Void aVoid) {
        // File deleted successfully 
        Log.e("firebasestorage", "onSuccess: deleted file");
    } 
}).addOnFailureListener(new OnFailureListener() { 
    @Override 
    public void onFailure(@NonNull Exception exception) {
        // Uh-oh, an error occurred! 
        Log.e("firebasestorage", "onFailure: did not delete file");
    } 
}); 

答案 1 :(得分:4)

试试这个我试过这个及其工作

 String storageUrl = "Chat-Images/1498804025000.png";
 StorageReference storageReference = FirebaseStorage.getInstance().getReference().child(storageUrl);
 storageReference.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");
         }
      });

答案 2 :(得分:2)

使用网址从Firebase存储中删除文件的代码段

StorageReference storageReference = FirebaseStorage.getInstance().getReferenceFromUrl("https://firebasestorage.googleapis.com/v0/b/***********************-5fac-45b6-bbda-ed4e8a3a62ab");
storageReference.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
    @Override
    public void onSuccess(Void aVoid) {
        // File deleted
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Error
    }
});

答案 3 :(得分:0)

如果您使用的是Kotlin,则代码如下:

Scaffold(
  appBar: AppBar(),
  resizeToAvoidBottomInset: false, //don't forget this!
  body: myStreamBuilder
)

答案 4 :(得分:0)

我认为您需要getStorage()才能使用getReferenceFromUrl()

例如:

FirebaseStorage.getInstance().getStorage().getReferenceFromUrl(fileURL);