gpt4 book ai didi

android - 完成 Firebase 函数(Uri 和字符串)Android Java 时出现 ClassCastException

转载 作者:行者123 更新时间:2023-12-05 00:20:22 28 4
gpt4 key购买 nike

我想在完成 onCompleteListenner 时创建一个调用 Cloud 函数(firebase 的东西)和 Android 应用程序代码的 Task 方法应用程序抛出一个 ClassCastException 有点“字符串不能转换为 Uri”。我应该怎么办?

这是一个需要的代码:

private Task<Uri> getProfilePicture(){
return functions
.getHttpsCallable("getProfilePicture")
.call()
.continueWith(task -> (Uri) task.getResult().getData())
.addOnCompleteListener(task -> {
String pfp = task.getResult().toString();
Uri pfp2 = Uri.parse(pfp);
setProfilePhoto(pfp2);
});
}

函数代码:
import * as functions from 'firebase-functions';
import admin = require('firebase-admin');
admin.initializeApp()

export const getProfilePicture = functions.https.onRequest((request, response) => {

const promise = admin.storage().bucket().file('usersPfp/aomkefQvkRQCuB66m6L9m94k9Bf1').getSignedUrl({
action: 'read',
expires: '03-09.2441'
})

const p2 = promise.then(GetSignedUrlResponse => {
const data = GetSignedUrlResponse[0]
return response.send({"data": data})
})

p2.catch(error =>{
console.log(error)
return response.status(500).send({"error": error})
})
})

还有一个异常(exception):
2020-01-19 15:41:04.150 28029-28029/com.happs.medrate E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.happs.medrate, PID: 28029
com.google.android.gms.tasks.RuntimeExecutionException: java.lang.ClassCastException: java.lang.String cannot be cast to android.net.Uri
at com.google.android.gms.tasks.zzu.getResult(Unknown Source:15)
at com.happs.medrate.viewmodel.main.AccountFragmentViewModel.lambda$getProfilePicture$1$AccountFragmentViewModel(AccountFragmentViewModel.java:76)
at com.happs.medrate.viewmodel.main.-$$Lambda$AccountFragmentViewModel$OKnCSppMNZTxWBSaV67HE9UL6mo.onComplete(Unknown Source:2)
at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to android.net.Uri
at com.happs.medrate.viewmodel.main.AccountFragmentViewModel.lambda$getProfilePicture$0(AccountFragmentViewModel.java:74)
at com.happs.medrate.viewmodel.main.-$$Lambda$AccountFragmentViewModel$gUAaj3fCSegHGtCnpDov7VKk9aQ.then(Unknown Source:0)
at com.google.android.gms.tasks.zzd.run(Unknown Source:5)
at android.os.Handler.handleCallback(Handler.java:789) 
at android.os.Handler.dispatchMessage(Handler.java:98) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6944) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

最佳答案

试试 continueWith(task -> Uri.parse(task.getResult().getData())) .

之后你不需要在 addOnCompleteListener 中解析它.

关于android - 完成 Firebase 函数(Uri 和字符串)Android Java 时出现 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59810216/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com