Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android image upload AWS Server

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?

like image 656
Rohan Kandwal Avatar asked Aug 06 '17 11:08

Rohan Kandwal


People also ask

How do I send pictures to AWS?

jpg . Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to upload your folders or files to. Choose Upload.


Video Answer


1 Answers

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.

like image 58
Saveen Avatar answered Oct 07 '22 09:10

Saveen