无法获取Firebase存储中上载的图像的实际下载网址 [英] Can't get actual download Url of an image uploaded in firebase storage

查看:52
本文介绍了无法获取Firebase存储中上载的图像的实际下载网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get the download url of the image uploaded to firebase database. But Task Uri imageURL = storageReference.getDownloadUrl(); doesn't gives the actual download URL of an image stored in the firebase storage i.e it gives this - com.google.android.gms.tasks.zzu@27da5837

getdownloadUrl()在已弃用的情况下不起作用:Uri imageUrl = storagereference.getdownloadUrl();//给出错误

getdownloadUrl() doesn't work in case of as it is deprecated: Uri imageUrl = storagereference.getdownloadUrl(); //Gives error

请在此处查看代码:

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

        }
    });

推荐答案

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

Calling getDownloadUrl() starts an asynchronous operation, which may take some time to complete. Because of this, your main code continues to run while the download URL is being retrieved to prevent blocking the user. Then when the download URL is available, your onSuccess gets called. Because of this, by the time your intent.putExtra("imageURL", imageUrl) now runs, the imageUrl = uri.toString() hasn't run yet.

要确认确实如此,我建议在代码中添加一些日志记录以仅显示流程,或在调试器中运行它,并在我上面指出的行上设置断点.

To see that this indeed true, I recommend putting some logging in the code to just show the flow, or running it in a debugger and setting breakpoints on the lines I indicated above.

要修复此问题,任何需要下载URL的代码都必须位于 onSuccess 内,或从此处进行调用.这不仅适用于此处,还适用于异步运行的所有代码,其中包括大多数现代云API.因此,我建议您花一些时间研究这种行为,以使您对以后的工作更加满意.

To fix it, any code that needs the download URL, needs to be inside the onSuccess, or be called from there. This applies not just here, but to all code that runs asynchronously, which includes most modern cloud APIs. So I recommend spending some time now studying this behavior, so that you're more comfortable with it going forward.

在您的代码中:

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

另请参阅:

这篇关于无法获取Firebase存储中上载的图像的实际下载网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆