【问题标题】:Can't get actual download Url of an image uploaded in firebase storage [duplicate]无法获取上传到 Firebase 存储中的图像的实际下载 URL [重复]
【发布时间】:2020-10-31 23:49:22
【问题描述】:

我正在尝试获取上传到 firebase 数据库的图片的下载网址。但是 Task Uri imageURL = storageReference.getDownloadUrl(); 没有给出存储在 firebase 存储中的图像的实际下载 URL,即它给出了这个 - com.google.android.gms.tasks.zzu@27da5837

getdownloadUrl() 在不推荐使用的情况下不起作用: Uri imageUrl = storagereference.getdownloadUrl(); //报错

请看这里的代码:

final StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("images").child(imageName);

    final UploadTask uploadTask = storageReference.putBytes(data);

    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle unsuccessful uploads
            Toast.makeText(CreateSnapActivity.this, "Upload Failed", Toast.LENGTH_SHORT).show();
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
            // taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.

            //Task<Uri> imageURL = storageReference.getDownloadUrl();  
            //Log.i("URL", imageURL.toString());        // gives this url - com.google.android.gms.tasks.zzu@27da5837 which is not correct


            Task<Uri> urlTask = storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                Log.i("Url", String.valueOf(uri));
                imageUrl = uri.toString();  //Gives the correct url but it cannot store this uri value outside this function i.e for Intent shown below outside this func.
               
                }
            });

            Toast.makeText(CreateSnapActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(CreateSnapActivity.this, ChooseUserActivity.class);
            intent.putExtra("imageName", imageName);
            intent.putExtra("imageURL", imageUrl);  
            intent.putExtra("message", captionEditText.getText().toString());
            startActivity(intent);

        }
    });

【问题讨论】:

  • " //Gives error" -> 你得到的确切错误是什么?如果是异常,还包括完整的堆栈跟踪。您可以通过单击其下方的edit 链接将两者都添加到您的问题中。

标签: android firebase firebase-realtime-database firebase-storage


【解决方案1】:

imageURL 是一项任务。您正在尝试打印一项任务。不是任务的结果。请尝试以下代码:

Task<Uri> imageURL = storageReference.getDownloadUrl();  
Log.i("URL", imageURL.toString());
Log.i("URL", imageURL.result.toString());

【讨论】:

    【解决方案2】:

    调用getDownloadUrl() 会启动一个异步操作,这可能需要一些时间才能完成。因此,在检索下载 URL 时,您的主代码会继续运行,以防止阻止用户。然后当下载 URL 可用时,您的 onSuccess 会被调用。因此,当您的 intent.putExtra("imageURL", imageUrl) 现在运行时,imageUrl = uri.toString() 尚未运行。

    要看到这确实是真的,我建议在代码中添加一些日志以仅显示流程,或者在调试器中运行它并在我上面指出的行上设置断点。

    要修复它,任何需要下载 URL 的代码都需要在 onSuccess 内,或者从那里调用。这不仅适用于这里,还适用于所有异步运行的代码,包括大多数现代云 API。因此,我建议现在花一些时间研究这种行为,以便您在以后更适应它。

    在您的代码中:

    final StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("images").child(imageName);
    
    final UploadTask uploadTask = storageReference.putBytes(data);
    
    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            Toast.makeText(CreateSnapActivity.this, "Upload Failed", Toast.LENGTH_SHORT).show();
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
            Task<Uri> urlTask = storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    imageUrl = uri.toString();
                    Toast.makeText(CreateSnapActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
    
                    Intent intent = new Intent(CreateSnapActivity.this, ChooseUserActivity.class);
                    intent.putExtra("imageName", imageName);
                    intent.putExtra("imageURL", imageUrl);  
                    intent.putExtra("message", captionEditText.getText().toString());
                    startActivity(intent);
               
                }
            });
        }
    });
    

    另见:

    【讨论】:

    • 非常感谢!问题解决了。我应该把意图代码放在 onSuccess 方法中。
    猜你喜欢
    • 2020-11-02
    • 2018-01-24
    • 1970-01-01
    • 2019-03-13
    • 2017-12-03
    • 2016-11-20
    • 2018-01-05
    • 1970-01-01
    • 2018-02-04
    相关资源
    最近更新 更多