Я пытаюсь загрузить изображение с камеры или галереи с помощью retrofit2. Он прекрасно работает до метода enqueue (), но после этого возвращает 400 плохих реквиатов с таким ответом: например, D / OkHttp: ������JFIF����������������C�� ....
![This is my server side request and response type information](https://i.stack.imgur.com/KjBDF.png)
вызов API:
@Multipart
@POST("feed/")
Call<ResponseBody> postFeedItem(@Part("location") RequestBody location,
@Part MultipartBody.Part m_Url);
Способ загрузки во фрагменте:
private void sharePost(){
File file = new File(globalPost);
RequestBody filePart = RequestBody.create(MediaType.parse("image/*"),file);
MultipartBody.Part body = MultipartBody.Part.createFormData("m_Url",file.getName(),filePart);
Timber.i(file.toString());
String loc = location.getText().toString();
RequestBody loc_req = RequestBody.create(MultipartBody.FORM,loc);
if(loc !=null && file != null){
Call<ResponseBody> call = ApiClient.getApiClient().create(UserClient.class).postFeedItem(loc_req,body);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Toast.makeText(getContext(),response.code(),Toast.LENGTH_LONG).show();
Timber.i(String.valueOf(response.code()));
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Timber.e(t.getMessage()+"onFailure()");
}
});
}
}
Результат из галереи или камеры:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
//Bundle bundle = data.getExtras();
// final Bitmap bmp = (Bitmap) data.getExtras().get("data");
//imageView.setImageBitmap(bmp);
Glide.with(this).load(imageFilePath).into(imageView);
//imageBitmap = bmp;
globalPost = imageFilePath;
share.setVisibility(View.VISIBLE);
} else if (requestCode == REQUEST_GALLERY) {
Uri selectedImageUri = data.getData();
imageView.setImageURI(selectedImageUri);
try {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(selectedImageUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
globalPost = picturePath;
cursor.close();
share.setVisibility(View.VISIBLE);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private String imageToString(Bitmap imageBitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
byte[] imgByte = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(imgByte,Base64.DEFAULT);
}