0
我嘗試使用Retrofit Mulitpart Android製作上傳圖像。但我仍然得到我的應用程序錯誤。Android - 上傳多部分翻新失敗或崩潰的圖像
這是如何得到的圖像
private final int SELECT_PHOTO = 1;
private void startGallery() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/png");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
這是onActivityResult,我要求上傳圖片
@Override
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
selectedImage = imageReturnedIntent.getData();
File file = new File(selectedImage.getPath());
RequestBody reqFile = RequestBody.create(okhttp3.MediaType.parse("image/png"), file);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body = MultipartBody.Part.createFormData("DocTyp01", file.getName(), reqFile);
getApi().uploadByCustomer(PrefHelper.getString(PrefKey.TOKEN), "DocTyp01", body)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Observer<GenericResponse>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
dismissProgressDialog();
Timber.e(e.getMessage());
}
@Override
public void onNext(GenericResponse response) {
dismissProgressDialog();
if (response.getCode() == 0) {
showSuccessDialog("Saving success", false);
}
}
});
}
}
}
這種方式是API服務
@Multipart
@POST(PORTAL_URL + "customerDocument/uploadByCustomer")
Observable<GenericResponse> uploadByCustomer(@Part("token") String token,
@Part("type") String type,
@Part MultipartBody.Part requestBodyFile);
有時候日誌沒有顯示錯誤,也沒有API響應,這讓我很難找到答案d錯誤。請幫我解決這個問題。謝謝。
你得到了什麼錯誤? – yosriz
來自API的響應是「文件未找到」,並且我在調試器上看到contentType爲空。它應該填充「DocTyp01」,如果我在Rest客戶端測試@yosriz –
請附上錯誤日誌 – yosriz