【问题标题】:getDownloadURL isn't inputting the link i needgetDownloadURL 没有输入我需要的链接
【发布时间】:2019-04-07 06:06:27
【问题描述】:

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

我是 firebase 和 android 开发的新手,所以我正在阅读有关存储的发行说明等,并注意到 in the notes 并在 downloadUrl 上找到了信息。我知道关于这个主题有很多问题,但很难将其应用于此代码。

“已弃用 StorageMetadata 类的 getDownloadUrl() 和 getDownloadUrls() 方法。改用 StorageReference.getDownloadUrl()。”

但我试图从存储在 firebase 存储中的元数据中提取 downloadUrl 链接

我可以成功地将图片上传到 firebase 存储,但是当我将 downloadUrl 放入 firebase DB 到我想要的节点时。它输入“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

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) 上遇到错误

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

【问题讨论】:

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


    【解决方案1】:

    调用getDownloadUrl() 返回一个Task,它从服务器异步获取下载URL。当它检索到下载 URL 时,它会调用 onComplete/onSuccess。这意味着下载 URL 值仅在 onComplete/onSuccess 方法中可用。任何需要下载 URL 的代码都必须在 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 中包含代码当然不会是非常可重用的。所以你可以考虑创建一个自定义回调接口,如图here。但是这个接口会和上面例子中的Task&lt;Uri&gt;接口很相似,所以我不确定它是否值得。


    有关如何通过拥有自己的自定义回调接口来保持代码与onComplete 分开的示例,请参阅getContactsFromFirebase() method return an empty list

    【讨论】:

      猜你喜欢
      • 2022-01-27
      • 1970-01-01
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多