AWS S3 Rest API with Android Retrofit V2 library, uploaded image is damaged

后端 未结 6 1566
生来不讨喜
生来不讨喜 2020-12-03 08:57

I\'m trying upload a Image from my Android APP to Amazon AWS S3 and I need use AWS Restful API.

I\'m using Retrofit 2

6条回答
  •  时光取名叫无心
    2020-12-03 09:36

    I have used Retrofit 2 resolve and I use Body instead of Part for your RequestBody in interface

    @PUT("")
    Call nameAPI(@Url String url, @Body RequestBody body);
    

    and java code

    // Prepare image file
    File file = new File(pathImg);
    RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpeg"), file);
    
    Call call = SingletonApiServiceS3.getInstance().getService().nameAPI(
            path,
           requestBody
    );
    call.enqueue(new Callback() {
        @Override
        public void onResponse(Call call, final Response response) {
    
            if (response.isSuccessful()) {
                // Your handling
            } else {
                // Your handling
           }
       }
    
       @Override
       public void onFailure(Call call, Throwable t) {
           Toast.makeText(getContext(), "onFailure : "+t.getMessage().toString(),Toast.LENGTH_SHORT).show();
       }
    });
    

提交回复
热议问题