task.getResult()抛出一个java.lang.ClassCastException异常。

问题描述 投票:0回答:1

我使用的是Firebase存储19.1.1和Firebase数据库19.2.1。

我使用了很多开发者推荐的方法来检索存储在Firebase存储中的图片的Uri,这是我的代码。

uploadTask.continueWith(new Continuation()
                {
                    @Override
                    public Object then(@NonNull Task task) throws Exception
                    {
                        if (!task.isSuccessful())
                        {
                            progressDialog.cancel();
                            Toast.makeText(FriendsChatActivity.this, ErrorOcc, Toast.LENGTH_SHORT).show();
                        }

                        return filePath.getDownloadUrl();
                    }
                }).addOnCompleteListener(new OnCompleteListener<Uri>()
                {
                    @Override
                    public void onComplete(@NonNull Task<Uri> task)
                    {
                        Uri downloadUrl = task.getResult();
                        myUrl = downloadUrl.toString();

                        Map messagePictureBody = new HashMap();
                            messagePictureBody.put("message", myUrl);
                            messagePictureBody.put("type", checker);
                            messagePictureBody.put("from", messageSenderID);
                            messagePictureBody.put("to", messageReceiverID);
                            messagePictureBody.put("messageID", messagePushID);
                            messagePictureBody.put("time", saveCurrentTime);
                            messagePictureBody.put("date", saveCurrentDate);

                        Map messageBodyDetails = new HashMap();
                        messageBodyDetails.put(messageSenderRef + "/" + messagePushID, messagePictureBody);
                        messageBodyDetails.put(messageReceiverRef + "/" + messagePushID, messagePictureBody);

                        RootRef.child("Users").child(messageSenderID).addValueEventListener(new ValueEventListener()
                        {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot)
                            {
                                fromName = dataSnapshot.child("name").getValue().toString();
                                online = dataSnapshot.child("online").getValue().toString();
                                progressDialog.cancel();
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError)
                            {

                            }
                        });
                    }
                });

而我的错误是:

EAndroidRuntime: FATAL EXCEPTION: main Process: com.eiskalterfreund.chweppen, PID: 520 java.lang.ClassCastException: com.google.android.gms.tasks.zzu cannot be cast to android.net.Uri at com.eiskalterfreund. chweppen.FriendsActivities.FriendsChatActivity$10.onComplete(FriendsChatActivity.java:562) at com.google.android.gms.tasks.zzj.run(Unknown Source:4) at android.os.Handler.handleCallback(Handler. java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:8016) at java.lang. reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)

android firebase android-studio firebase-realtime-database firebase-storage
1个回答
1
投票
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        if (taskSnapshot.getMetadata() != null) {
            if (taskSnapshot.getMetadata().getReference() != null) {
                Task<Uri> result = taskSnapshot.getStorage().getDownloadUrl();
                result.addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        String imageUrl = uri.toString();
                        //createNewPost(imageUrl);
                    }
                });
            }
        }
    }});
© www.soinside.com 2019 - 2024. All rights reserved.