我正在尝试使用url将文件从android应用程序上传到firebase存储,而不是使用改型的SDK。这是我上传图片的方法,
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方法,也没有出现图像。