How to Get PNG Icon Byte Array (or PNG Icon File) for a Common File Extension using Java

Cloudmersive
3 min readOct 18, 2022

--

File icons are ubiquitous online, often used alongside “input file” fields on a website to help users avoid attaching the wrong file extension. Getting ahold of file icons can be a bit challenging, however, as you’re often forced to download PNG/JPG files from websites you aren’t sure you can trust. Thankfully, our secure PNG Icon API makes it possible to generate a PNG Icon Byte Array using only your file’s extension as a string (up to 4 AlphaNumeric characters — e.g., docx, pdf, xlsx, etc.) and will allow you to customize the desired width of the output byte array (preserving aspect ratio). Alternatively, through the same Cloudmersive Document Conversion API endpoint, you can also get a regular PNG Icon File for your input file extension (also with the option to customize width of the output image).

Following the instructions below, you can easily take advantage of either PNG Icon API & structure your API call using the ready-to-run Java code examples we’ve included for your convenience.

To get started, let’s first install the API client. We can begin by adding a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next, we can add a reference to the pom.xml dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Once installation is complete, we can add the imports and call our first function. Before we do that, however, we’ll first need to get ahold of a free-tier Cloudmersive API key; we can do so by visiting the Cloudmersive website and registering a free account (which comes with a limit of 800 API calls per month and zero additional commitments).

The below code will call the PNG Icon Byte Array generation API. Make sure to include your API key & file path in their respective fields:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
String fileExtension = "fileExtension_example"; // String | Required; The file extension to be used for the icon. Limited to 4 AlphaNumeric characters.
Integer iconSize = 56; // Integer | Optional; The desired width of the icon, preserving its aspect ratio.
try {
GetFileTypeIconResult result = apiInstance.convertDocumentGetFileTypeIconAdvanced(fileExtension, iconSize);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentGetFileTypeIconAdvanced");
e.printStackTrace();
}

The below code will instead call the PNG Icon File API:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
String fileExtension = "fileExtension_example"; // String | Required; The file extension to be used for the icon. Limited to 4 AlphaNumeric characters.
Integer iconSize = 56; // Integer | Optional; The desired width of the icon, preserving its aspect ratio.
try {
byte[] result = apiInstance.convertDocumentGetFileTypeIcon(fileExtension, iconSize);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentGetFileTypeIcon");
e.printStackTrace();
}

With that, you’re all set — no more code required.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet