0

I am trying to upload an image from camera or gallery using retrofit2 It runs perfect until enqueue() method but after that it returns a 400 bad requeat with this response: like this: D/OkHttp: ������JFIF����������������C�� ....

This is my server side request and response type information

Api call:

  @Multipart
@POST("feed/")
Call<ResponseBody> postFeedItem(@Part("location") RequestBody location,
                                @Part MultipartBody.Part m_Url);

Upload method in Fragment:

 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()");
            }
        });
    }
}

Result from gallery or camera:

   @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);
}
Dilip Krishna
  • 139
  • 3
  • 16
  • Did you try with Postman client? If not, please confirm it by sending request to your Django API from Postman. If that works please [edit] the question and add screenshot of Postman request as well. This way it'll be easier to understand the problem. And more thing your API returning "400 Bad request" error. So, you need to understand it first. Refer this StackOverflow [thread](https://stackoverflow.com/q/19671317/5180017) to understand it. – Shashanth Sep 17 '18 at 05:05
  • 1
    It is working now!!!! I did not change any code but once it showed above error and now it's working fine. I didn't understood what happened but it worked! – Dilip Krishna Sep 20 '18 at 13:58

0 Answers0