Java Box SDK - Thumbnail Not Retrieved On First Request
AnsweredI am using the Java Box SDK and I cannot retrieve the thumbnail of an image on my first request. An empty byte array is always returned for the first request. Subsequent requests for that file's thumbnail after waiting a short period (about 1 second) always work. Because that short delay after the first call is needed, I can't simply call getThumbnail() twice if the first request doesn't return anything.
Am I doing something wrong? Has anyone else experienced this?
public byte[] getThumbnail(String boxId){
BoxFile file = new BoxFile(this.api, boxId);
byte[] thumbnailBytes = file.getThumbnail(BoxFile.ThumbnailFileType.PNG, 32, 32, 32, 32);
return thumbnailBytes;
}
-
Hey, thanks for the reply. I really don't have any ideas what is causing this but I can share a little more about how we are using the API.
We have a web application where users can upload files and we do some editing to the file. The edited file ends up as a .TIFF but the same behavior can be seen with a .PNG. The files are uploaded via the API like this (some error checking and a call to our database removed for brevity);
public String uploadFile(String parentFolderId, String fileName, byte[] fileBytes, String username){ String resultingId = null; BoxFolder myFolder = new BoxFolder(this.api, parentFolderId); InputStream fileInput = new ByteArrayInputStream(fileBytes); BoxFile.Info fileInfo = myFolder.uploadFile(fileInput, fileName); resultingId = fileInfo.getID(); return resultingId; }
parentFolderId is the box ID for the folder we are storing these files in. The files upload correctly and are visible in box's UI. I don't think how we upload the file is an issue but I'm trying to give you any helpful context I can.
Here is a main method that also demonstrates the behavior for me. This approach uses a developer token instead of the config.json. If I debug this and sit on the "waiting..." statement for a few seconds (maybe 5 seconds), the subsequent thumbnail request always works. It doesn't matter how long I wait between the uploadFile() and the first getThumbnail() call - it always returns me an empty array. Box folder ID, developer token and test image path have been masked.
public static void main(String[] args) throws IOException{ System.out.println("starting..."); String parentFolderId = "###########"; String filePath = "C:/path/to/an/image.png"; File file = new File(filePath); FileInputStream fis = new FileInputStream(file); String filename = "mytestfile.png"; BoxAPIConnection api = new BoxAPIConnection("################################"); BoxFolder myFolder = new BoxFolder(api, parentFolderId); BoxFile.Info fileInfo = myFolder.uploadFile(fis, filename); String resultingId = fileInfo.getID(); BoxFile boxfile = new BoxFile(api, resultingId); byte[] thumbnailBytes = boxfile.getThumbnail(BoxFile.ThumbnailFileType.PNG, 32, 32, 32, 32); System.out.println("waiting..."); thumbnailBytes = boxfile.getThumbnail(BoxFile.ThumbnailFileType.PNG, 32, 32, 32, 32); System.out.println("done"); }
Other requests using the API on this file work on the first request (e.g. getting the full size image or the file name in box) but only the thumbnail is giving us trouble.
-
Hello,
Sorry for the delayed response. Take a look at the thumbnail docs at https://developer.box.com/reference. In the case of a 202 status code a thumbnail is not yet generated and you would have to retry the request after a certain amount of time. I would check if a 202 status code is returned from the call. If this is the case, then what is happening is expected behavior. One way to check is to manually make a Postman request. This is a collection of Box API Endpoints for postman: https://developer.box.com/docs/box-postman-collection. The other option is to go into the getThumbnail() method in your project (https://github.com/box/box-java-sdk/blob/master/src/main/java/com/box/sdk/BoxFile.java) and print out the response to check the status code.
Please sign in to leave a comment.
Comments
4 comments