我正在尝试使用url而不是使用Retrofit的SDK从android应用上传文件到firebase存储。这是我的上传图片方法,
fun uploadImageToUrl() {
val url = "https://firebasestorage.googleapis.com/v0/b/xxxxxxx-xxxx.appspot.com/o/${bean.firebaseUid}/idProofs/${bean.documentTitle}.jpg/"
var client = OkHttpClient.Builder().build()
val retrofit = Retrofit.Builder()
.baseUrl(url).addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
var service = retrofit.create(UploadService::class.java)
var params = HashMap<String, RequestBody>()
var reqFile = RequestBody.create(MediaType.parse("image/*"), photoFile)
var header = "Bearer ${bean.firebaseToken}"
var body = MultipartBody.Part.createFormData("image", bean.documentTitle, reqFile)
var response = service.uploadDocument(header, body)
response.enqueue(object: Callback<ResponseBody> {
override fun onResponse(call: Call<ResponseBody>?, response: Response<ResponseBody>?) {
Log.e("Upload Response", response!!.message())
}
override fun onFailure(call: Call<ResponseBody>?, t: Throwable?) {
Log.e("Upload Failed", t!!.localizedMessage)
}
})
}
这是服务界面中的方法。
interface UploadService {
@Multipart
@Headers("Accept: application/json")
@POST("/?alt=media")
fun uploadDocument(@Header("Authorization") header: String, @Part image: MultipartBody.Part): retrofit2.Call<ResponseBody>
}
但没有发生任何事情,没有错误消息,onResponse,onFailure方法没有被调用,图像没有出现。
答案 0 :(得分:1)
我的坏。网址应该是这样的
https://firebasestorage.googleapis.com/v0/b/xxxxxxx-xxxx.appspot.com/o/ $ {bean.firebaseUid}%2FidProofs%2F $ {bean.documentTitle} .JPG /
而不是
https://firebasestorage.googleapis.com/v0/b/xxxxxxx-xxxx.appspot.com/o/ $ {bean.firebaseUid} / idProofs / $ {bean.documentTitle} .JPG /
我不知道为什么,但它正在发挥作用。