getDownloadURL没有输入我需要的链接 [英] getDownloadURL isn't inputting the link i need

查看:78
本文介绍了getDownloadURL没有输入我需要的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试获取downloadUrl链接并将其放入数据库中的"profile/imageURL"节点.

Trying to get the downloadUrl link and put it into the "profile/imageURL" node on database.

我是Firebase和android开发的新手,因此我正在阅读存储中的发行说明等,并注意到中的"nofollow noreferrer>",并找到了关于downloadUrl的信息.我了解有关此主题的问题很多,但很难将其应用于此代码.

I'm new to firebase and android development, so i was reading through release notes etc on storage and noticed in the notes and found information on downloadUrl. I understand there are plenty of questions on this topic but it's hard to apply it to this code.

不推荐使用StorageMetadata类的getDownloadUrl()和getDownloadUrls()方法.请改用StorageReference.getDownloadUrl()."

"Deprecated the getDownloadUrl() and getDownloadUrls() methods of the StorageMetadata class. Use StorageReference.getDownloadUrl() instead."

但是我试图不从firebase存储中存储的元数据中拉出downloadUrl链接

But i'm trying to pull the downloadUrl link not from the metadata stored in firebase storage

我可以成功地将图片上传到Firebase存储器,但是当我将downloadUrl放入Firebase DB到所需的节点中时.它输入"com.google.android.gms.tasks.zzu@xxxxx"

I can successfully upload the picture to firebase storage but when i put the downloadUrl into firebase DB into the node i want. it inputs "com.google.android.gms.tasks.zzu@xxxxx"

final String downloadUrl = task.getResult().getStorage().getDownloadUrl().toString();

                            UserProfileRef.child("profile").child("imageURL").setValue(downloadUrl)

我尝试了以下2个代码,此代码输出uri com.google.xxxx

I have tried the following 2 codes, This code outputs the uri com.google.xxxx

protected void onActivityResult( int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode==Gallery_Pick && resultCode==RESULT_OK && data!=null)
    {
        Uri ImageUri = data.getData();

        CropImage.activity()
                .setGuidelines(CropImageView.Guidelines.ON)
                .setAspectRatio(1,1)
                .start(this);
    }
    if (requestCode==CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
    {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);

        if(resultCode == RESULT_OK)
        {

            loadingBar.setTitle("Profile Image");
            loadingBar.setMessage("Please wait, while we are uploading image...");
            loadingBar.setCanceledOnTouchOutside(true);
            loadingBar.show();


            Uri resultUri = result.getUri();

            StorageReference filePath = UserProfileImageRef.child(currentUserID + ".jpg");

            filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

                    if(task.isSuccessful())
                    {
                        Toast.makeText(ProfileActivity.this, "Profile Image stored Successfully to the the storage...", Toast.LENGTH_SHORT).show();

                        final String downloadUrl = task.getResult().getStorage().getDownloadUrl().toString();

                        UserProfileRef.child("profile").child("imageURL").setValue(downloadUrl)
                                .addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task)
                                    {
                                        if(task.isSuccessful())
                                        {
                                            Intent selfIntent = new Intent(ProfileActivity.this, ProfileActivity.class);
                                            startActivity(selfIntent);


                                            Toast.makeText(ProfileActivity.this, "Profile image stored to firebase database successfully.", Toast.LENGTH_SHORT).show();
                                            loadingBar.dismiss();
                                        }
                                        else
                                        {
                                            String message = task.getException().getMessage();
                                            Toast.makeText(ProfileActivity.this, "Error Occured..." + message, Toast.LENGTH_SHORT).show();
                                            loadingBar.dismiss();
                                        }
                                    }
                                });
                    }
                }
            });
        }
        else
        {
            Toast.makeText(this, "Error Occured: Image can't be cropped, try again..", Toast.LENGTH_SHORT).show();
            loadingBar.dismiss();
        }
    }
}

在其他用户的问题帮助下尝试了第二个代码,并尝试以这种方式解决.虽然我在.setValue(downloadUrl)上出现错误

This second code was tried with the help of other users questions and trying to solve it that way. Although i get an error on .setValue(downloadUrl)

protected void onActivityResult( int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode==Gallery_Pick && resultCode==RESULT_OK && data!=null)
    {
        Uri ImageUri = data.getData();

        CropImage.activity()
                .setGuidelines(CropImageView.Guidelines.ON)
                .setAspectRatio(1,1)
                .start(this);
    }
    if (requestCode==CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
    {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);

        if(resultCode == RESULT_OK)
        {

            loadingBar.setTitle("Profile Image");
            loadingBar.setMessage("Please wait, while we are uploading image...");
            loadingBar.setCanceledOnTouchOutside(true);
            loadingBar.show();


            Uri resultUri = result.getUri();

            final StorageReference filePath = UserProfileImageRef.child(currentUserID + ".jpg");

            filePath.putFile(resultUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {

                            final String downloadUrl = uri.toString();
                        }
                    });

                    UserProfileRef.child("profile").child("imageURL").setValue(downloadUrl)
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task)
                                {
                                    if(task.isSuccessful())
                                    {
                                        Intent selfIntent = new Intent(ProfileActivity.this, ProfileActivity.class);
                                        startActivity(selfIntent);


                                        Toast.makeText(ProfileActivity.this, "Profile image stored to firebase database successfully.", Toast.LENGTH_SHORT).show();
                                        loadingBar.dismiss();
                                    }
                                    else
                                    {
                                        String message = task.getException().getMessage();
                                        Toast.makeText(ProfileActivity.this, "Error Occured..." + message, Toast.LENGTH_SHORT).show();
                                        loadingBar.dismiss();
                                    }
                                }
                            });
                }
            });
        }
        else
        {
            Toast.makeText(this, "Error Occured: Image can't be cropped, try again..", Toast.LENGTH_SHORT).show();
            loadingBar.dismiss();
        }
    }
}

推荐答案

调用getDownloadUrl()返回一个Task,该异步从服务器获取下载URL.检索下载URL后,它将调用onComplete/onSuccess.这意味着下载URL值仅在onComplete/onSuccess方法内部可用.需要下载URL的所有代码都必须在onComplete/onSuccess内部.

Calling getDownloadUrl() returns a Task that asynchronously gets the download URL from the server. When it has retrieved the download URL, it calls onComplete/onSuccess. This means the download URL value is only available inside the onComplete/onSuccess method. Any code that needs the download URL needs to be inside onComplete/onSuccess.

类似这样:

filePath.putFile(resultUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {

                final String downloadUrl = uri.toString();
                UserProfileRef.child("profile").child("imageURL").setValue(downloadUrl)
                  .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        loadingBar.dismiss();
                        if(task.isSuccessful()) {
                            Intent selfIntent = new Intent(ProfileActivity.this, ProfileActivity.class);
                            startActivity(selfIntent);
                            Toast.makeText(ProfileActivity.this, "Profile image stored to firebase database successfully.", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            String message = task.getException().getMessage();
                            Toast.makeText(ProfileActivity.this, "Error Occured..." + message, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }
});


现在,将代码包含在onSuccess中当然不是非常可重用的.因此,您可以考虑创建自定义的回调界面,如此处所示.但是该接口与上例中的Task<Uri>接口非常相似,因此我不确定是否值得这样做.


Now having the code inside the onSuccess is of course not going to be very reusable. So you may consider creating a custom callback interface as shown here. But the interface will be quite similar to the Task<Uri> interface in the example above, so I'm not sure it is worth the effort.

有关如何通过拥有自己的自定义回调接口仍将代码与onComplete分开的示例,请参见

For an example of how to still keep the code separate from the onComplete by having your own custom callback interface, see getContactsFromFirebase() method return an empty list.

这篇关于getDownloadURL没有输入我需要的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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