getDownloadURL没有输入我需要的链接

时间:2018-11-03 02:58:19

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

尝试获取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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

调用<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div id='vrview'> <iframe src="https://storage.googleapis.com/vrview/2.0/embed?image=beach.jpeg&is_stereo=false"> </iframe> </div> <script> window.addEventListener('load', onVrViewLoad); function onVrViewLoad() { // Selector '#vrview' finds element with id 'vrview'. var vrView = new VRView.Player('#vrview', { image: 'beach.jpeg', is_stereo: false }); } </script> <script src="https://storage.googleapis.com/vrview/2.0/build/vrview.min.js"></script> </body> </html>返回一个getDownloadUrl(),该异步地从服务器获取下载URL。检索下载URL后,它将调用Task / onComplete。这意味着下载URL值仅在onSuccess / onComplete方法内可用。需要下载URL的所有代码都必须位于onSuccess / onComplete内。

类似这样:

onSuccess