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

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

问题描述

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

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

我是 firebase 和 android 开发的新手,所以我正在阅读有关存储的发行说明等,并注意到 在注释 中找到了有关 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 界面非常相似,因此我不确定是否值得付出努力.


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 分开的示例,请参阅 getContactsFromFirebase() 方法返回一个空列表.

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天全站免登陆