Upload Image to Android AWS Server

Using the following code to upload an image to a server -

final InputStream fileInputStream = MyApplication.getInstance().getContentResolver().openInputStream(imageFile); bitmap = BitmapFactory.decodeStream(fileInputStream); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); final byte[] bitmapData = byteArrayOutputStream.toByteArray(); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); // Add binary body if (bitmap != null) { ContentType contentType = ContentType.create("image/png"); builder.addBinaryBody("", bitmapData, contentType, ""); final HttpEntity httpEntity = builder.build(); StringRequest request = new StringRequest(Request.Method.PUT, url, new Response.Listener<String>() { @Override public void onResponse(String response) { } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }) { @Override public byte[] getBody() throws AuthFailureError { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { httpEntity.writeTo(bos); } catch (IOException e) { VolleyLog.e("IOException writing to ByteArrayOutputStream"); } return bos.toByteArray(); } }; 

What is image loading but adding body header to file

 --0Iw7PkPg_BhQghFGBR1_lBhO1RaCaBsZJ-U Content-Disposition: form-data; name=""; filename="" Content-Type: image/png âPNG .... --0Iw7PkPg_BhQghFGBR1_lBhO1RaCaBsZJ-U-- 

If I remove this specific content from the file, the image can easily be used as a PNG. Is there a way to upload a PNG file to the server?

+8
android upload image amazon-web-services android-volley
source share
1 answer

I have the same problem trying to upload image to AWS server. I sent it as an octet stream . I used modification 2.2. Note If we used the Octet stream, then there is no need to do it as a Multipart request.

 @PUT Observable<Response<Void>> uploadMedia( @Header("Content-Type") String contentType, @Header("filetype") String FileType, @Url String urlPath, @Body RequestBody picture); private void UploadSignedPicture(String url, File filename, String mediaUrl) { mAmazonRestService.uploadMedia("application/octet-stream", "application/octet-stream", url, mAppUtils.requestBody(filename)). subscribeOn(mNewThread). observeOn(mMainThread). subscribe(authenticateResponse -> { if (this.isViewAttached()) { if (authenticateResponse.code() == ApiConstants.SUCCESS_CODE) else } }, throwable -> { if (isViewAttached()) getMvpView().showServerError(this, throwable); } }); } 

Most importantly, how do you create a query:

 @NonNull public RequestBody requestBody(File filename) { InputStream in = null; byte[] buf = new byte[0]; try { in = new FileInputStream(new File(filename.getPath())); buf = new byte[in.available()]; while (in.read(buf) != -1) ; } catch (IOException e) { e.printStackTrace(); } return RequestBody .create(MediaType.parse("application/octet-stream"), buf); } 

Thank you for helping this.

+2
source share

All Articles