Android image upload AWS Server
Asked Answered
L

1

8

Using following code to upload image on 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();
        }
      };

Which is uploading image fine but adding body header in the file

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

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

Lemieux answered 6/8, 2017 at 11:0 Comment(1)
Try using amazon sdk transfer utilityFerino
C
3

I have same problem tried to upload image to AWS server. I have sent it as a octet-stream. I have used retrofit 2.2. Note : If we used Octet-stream then there is no need to make 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 important how you create a request :

@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);
}

Thanks hope this will help you.

Carpetbagger answered 19/8, 2017 at 17:31 Comment(3)
I know how to do this in Retrofit but the issue is that the project was developed using VolleyLemieux
I have seen you are using multipart.So, it could be other way same in volley too.Carpetbagger
That's the issue, I can't find other way. Since the whole app is written in volley, can't just change the library nowLemieux

© 2022 - 2024 — McMap. All rights reserved.