0

I tried to get a direct url by image, which I uploading to imgbb.com with POST

It's a json, which I should to get when I send a post to imgbb https://api.imgbb.com/

{
  "data": {
    "id": "2ndCYJK",
    "title": "c1f64245afb2",
    "url_viewer": "https://ibb.co/2ndCYJK",
    "url": "https://i.ibb.co/w04Prt6/c1f64245afb2.gif",
    "display_url": "https://i.ibb.co/98W13PY/c1f64245afb2.gif",
    "size": "42",
    "time": "1552042565",
    "expiration":"0",
    "image": {
      "filename": "c1f64245afb2.gif",
      "name": "c1f64245afb2",
      "mime": "image/gif",
      "extension": "gif",
      "url": "https://i.ibb.co/w04Prt6/c1f64245afb2.gif",
    },
    "thumb": {
      "filename": "c1f64245afb2.gif",
      "name": "c1f64245afb2",
      "mime": "image/gif",
      "extension": "gif",
      "url": "https://i.ibb.co/2ndCYJK/c1f64245afb2.gif",
    },
    "medium": {
      "filename": "c1f64245afb2.gif",
      "name": "c1f64245afb2",
      "mime": "image/gif",
      "extension": "gif",
      "url": "https://i.ibb.co/98W13PY/c1f64245afb2.gif",
    },
    "delete_url": "https://ibb.co/2ndCYJK/670a7e48ddcb85ac340c717a41047e5c"
  },
  "success": true,
  "status": 200
}

I tried to creat a POJO but my pojo converter says to me that it's an incorect JSON. When I try to get a response body, I can't find the url from there.

My code is:

in MainActivity

private void uploadToServer(Bitmap bitmap, Context context) throws IOException {
        //Convert bitmap to byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        byte[] bitmapData = bos.toByteArray();
        //create file
        String currentDate = new SimpleDateFormat("ddMMyyyy_HHmmss", Locale.getDefault()).format(new Date());
        File f = new File(context.getCacheDir(), "temp_"+currentDate );
        f.createNewFile();
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(bitmapData);
        fos.flush();
        fos.close();

        Retrofit retrofit = RetroClient.getRetrofitClient(context);
        UploadApi uploadAPIs = retrofit.create(UploadApi.class);

        RequestBody requestFile =  RequestBody.create(MediaType.parse("multipart/form-data"), f);
        MultipartBody.Part body = MultipartBody.Part.createFormData("image", f.getName(), requestFile);


        Call<ResponseBody> call = uploadAPIs.uploadImage(RetroClient.KEY_API,body);

        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    Toast.makeText(context, "I send photo", Toast.LENGTH_SHORT);
                    response.body();
                }
                else {
                    Toast.makeText(context, "response isn't successful", Toast.LENGTH_SHORT);
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(context, "on failure", Toast.LENGTH_SHORT);
            }
        });
    }

Interface

public interface UploadApi {
    @Multipart()
    @POST("/1/upload?expiration=100&key=")
    Call<ResponseBody> uploadImage(@Query("key") String key, @Part() MultipartBody.Part file );
}
ecm
  • 2,583
  • 4
  • 21
  • 29
  • Do away with `bos` stuff and change `bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);` to `bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);`. No need for an intermediate ByteArrayOutputStream. – blackapps Sep 12 '21 at 06:25
  • Also remove`f.createNewFile();`. – blackapps Sep 12 '21 at 06:26
  • Check out this blog post for an easy and understandable solution: https://mboot.herokuapp.com/post/25 – McPringle Sep 12 '21 at 06:29
  • your json is not correct. remove comma(,) . There is a comma at the end of "url": "https://i.ibb.co/2ndCYJK/c1f64245afb2.gif", remove the comma (, ) from end of url tag. validate your json from https://jsoncompare.com/#!/simple/fullscreen/ – Vinay Sep 12 '21 at 07:03
  • 1
    @vinay6kr Your answer was helped me! Thank you! =) – hatoritotori Sep 15 '21 at 10:16
  • @hatoritotori please upvote for this solution . – Vinay Sep 16 '21 at 05:33

0 Answers0