Getting started - SoKnight/ImgBB-Java-SDK GitHub Wiki

Introduction

Before we starts, I should to say you that you should to read the next pages:

Then we can start.

Generating an upload parameters

Firstly you need to generate an upload parameters:

// converting an image file to base64 encoded string
File file = new File("my-image.png");
String base64 = ImageBase64Converters.fromImageFile(file);

// building upload parameters instance
// sure, change the 'API KEY' to your real API key
UploadParameters params = new UploadParameters.Builder()
        .apiKey("API KEY")
        .imageBase64(base64)
        .expirationTime(ExpirationTime.fromLong(60))
        .build();

Sending an upload request

You need to send a POST-request to the ImgBB API Service to upload your image on this hosting.

My SDK presents the ImgbbUploadClient as easily way to execute an upload POST-requests:

// using previously created parameters
// method #upload(...) returns an optional uploading response (see below)
OptionalResponse uploadingResponse = ImgbbUploadClient.upload(params);

And now we can handle received uploading response, see next step!

Handling an uploading response

In the previous step we get an uploading parameters as OptionalParameters. This class imitate the java.util.Optional and presents two main methods:

  • get() - Returns internal response data object if it's present.
  • isPresent() - Checks if internal response data is presented or not. (true/false)
// checking if the image is uploaded
if(!uploadingResponse.isPresent() || !uploadingResponse.get().isUploadedSuccess()) {
    System.err.println("Image uploading failed!");
    return;
}

// getting the direct url to uploaded image
String url = uploadingResponse.get().getResponseData().getImageUrl();
System.out.println("Image was successfully uploaded: " + url);

The full information about this response class you can find there.