You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Firebase Storage上传文件后如何获取下载链接?getDownloadUrl()失效

Fix: getDownloadUrl() Not Working in Firebase Storage 16.0.1+

Ah, I’ve hit this exact roadblock when upgrading Firebase Storage versions—total bummer when that familiar method suddenly stops working! The issue is that taskSnapshot.getDownloadUrl() was deprecated starting with Firebase Storage 16.x, so we need to adjust our approach to fetch the download URL.

Here’s the Correct Way to Get the Download URL Now:

Instead of calling getDownloadUrl() directly on the TaskSnapshot, you first grab the StorageReference from the snapshot, then call getDownloadUrl() on that reference. This method returns a Task<Uri>, so you’ll need to attach success/failure listeners to handle the result.

Java Example:

// Assume you have an upload task setup like this
UploadTask uploadTask = storageRef.putFile(file);

uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        // Get the StorageReference associated with the uploaded file
        StorageReference fileRef = taskSnapshot.getStorage();
        
        // Fetch the download URL
        fileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri downloadUri) {
                String downloadUrl = downloadUri.toString();
                // Use this URL as needed (display, save to database, etc.)
                Log.d("FirebaseStorage", "File download URL: " + downloadUrl);
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                // Handle errors getting the URL
                Log.e("FirebaseStorage", "Failed to retrieve download URL", e);
            }
        });
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        // Handle upload failures first
        Log.e("FirebaseStorage", "Upload failed", e);
    }
});

Kotlin Example:

val uploadTask = storageRef.putFile(file)

uploadTask.addOnSuccessListener { taskSnapshot ->
    val fileRef = taskSnapshot.storage
    
    fileRef.downloadUrl.addOnSuccessListener { downloadUri ->
        val downloadUrl = downloadUri.toString()
        // Use the download URL here
        Log.d("FirebaseStorage", "File download URL: $downloadUrl")
    }.addOnFailureListener { e ->
        // Handle URL retrieval errors
        Log.e("FirebaseStorage", "Failed to get download URL", e)
    }
}.addOnFailureListener { e ->
    // Handle upload errors
    Log.e("FirebaseStorage", "Upload failed", e)
}

Why This Changed:

Firebase’s team adjusted the API design to align with the logical ownership of the download URL—since the URL belongs to the stored file itself (the StorageReference), not just the snapshot of the upload task. This makes the API more consistent and flexible, especially if you need to fetch the URL later (not just right after upload).

Quick Tip:

Make sure all your Firebase dependencies are on matching versions! Mixing different version numbers (e.g., using firebase-storage:16.0.1 with an older firebase-core version) can cause unexpected bugs and compatibility issues.

内容的提问来源于stack exchange,提问作者Sheikh Hasib

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:39:28