0

I'm trying to send the POST API request to the Server like below and I'm getting the successful response code - 200, but when I try to decode the InputStream to Bitmap, then only the API requests get failed status code - 400, what went wrong with this approach

   InputStream inputStream = null;

                try {
                    inputStream = getContentResolver().openInputStream(imgUri);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                RequestBody requestBody = RequestBodyUtil.create(MediaType.get("image/" + imgType), inputStream);
          
                RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                        .addFormDataPart("messaging_product", “help”)
                        .addFormDataPart("file", "image/" + imgType, requestBody)
                        .addFormDataPart("type", "image/" + imgType)
                        .build();
    
                //---#####if I remove the below line, then everything working fine #####
                Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
                Log.d(TAG, "onClick: ");


                Call<ModelGraphResponse> call = RetrofitInstance.getRetrofitClient().create(IService.class).uploadImageToGraph(name, body);
             
              
                call.enqueue(new Callback<ModelGraphResponse>() {
                    @Override
                    public void onResponse(Call<ModelGraphResponse> call, retrofit2.Response<ModelGraphResponse> response) {
                        Log.d(TAG, "onResponse: ");
                        if (response.isSuccessful()) {
                           
                        } else {
                                                           }

                    }

                    @Override
                    public void onFailure(Call<ModelGraphResponse> call, Throwable t) {
                        Log.d(TAG, "onFailure: ");
                        button.setVisibility(View.VISIBLE);
                        progressBar.setVisibility(View.INVISIBLE);
                        textView.setText("Failed to Upload..");
                    }
                });
8
  • 1
    Why do you try to convert an inputstream to bitmap? Commented Dec 22, 2022 at 8:07
  • And what is it that you want to do with the bitmap? Commented Dec 22, 2022 at 8:08
  • And how does your imgUri look like.? Commented Dec 22, 2022 at 8:09
  • Which 'request' is is in your POST request? Commented Dec 22, 2022 at 8:12
  • Thank you for your question, there are my answers, I want to convert to byte[] and store them in the sqllite databse, Commented Dec 22, 2022 at 8:22

1 Answer 1

0

Try this once

URL url = new URL(//your URL//);
 try { 
HttpURLConnection connection  = (HttpURLConnection) url.openConnection();

InputStream is = connection.getInputStream();
 } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
//YOUR CODE

Bitmap img = BitmapFactory.decodeStream(is, null, options);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response, what is the URL here, Base Url ?
your Image URL @Prasath

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.