2
votes

I have been working on an app which I have list of customers and I am displaying the customer as a list (using container instead of list as explained in codename one). From the list with the leading label I click on the item and it goes to detail page where I display customer picture and other info. Here is my code to get image from url (amazon s3 bucket)

private Image getUserProfilePicture(String imgName)
{
Image defaultImage = FromResource("DefaultPhoto.png");
int width = defaultImage.getWidth();
int height = defaultImage.getHeight();

String imgUrl = "image from s3 bucket";
EncodedImage placeHolder = (EncodedImage)defaultImage;
Image roundMask = Image.createImage(width, height, 0xff000000);
Graphics gr = roundMask.getGraphics();
gr.setColor(0xffffff);
gr.fillArc(0, 0, width, height, 0, 360);
URLImage.ImageAdapter ada = URLImage.createMaskAdapter(placeHolder);
//image is deleted from storage before load
Image profileImage = URLImage.createToStorage(placeHolder, imgName, 
imgUrl, ada);
return profileImage;
}

There are customers whom have no images to begin with, I do see the default image. When I go back to the list and click again on the same customer, I get no image displayed. Empty white space. Instead I am getting the follow error message in the debugger window.

[EDT] 0:0:0,1 - Exception: java.lang.IllegalArgumentException - create image failed for the given image data of length: 306 java.lang.IllegalArgumentException: create image failed for the given image data of length: 306 at com.codename1.ui.Image.createImage(Image.java:655) at com.codename1.ui.EncodedImage.getInternal(EncodedImage.java:366) at com.codename1.ui.EncodedImage.getInternalImpl(EncodedImage.java:341) at com.codename1.ui.EncodedImage.getWidth(EncodedImage.java:512) at com.codename1.ui.URLImage$ScaleToFill.adaptImage(URLImage.java:116) at com.codename1.ui.URLImage$DownloadCompleted.actionPerformed(URLImage.java:239) at com.codename1.ui.URLImage$4.onSucess(URLImage.java:302) at com.codename1.ui.URLImage$4.onSucess(URLImage.java:298) at com.codename1.util.CallbackDispatcher.run(CallbackDispatcher.java:53) at com.codename1.ui.Display.processSerialCalls(Display.java:1155) at com.codename1.ui.Display.edtLoopImpl(Display.java:1099) at com.codename1.ui.Display.mainEDTLoop(Display.java:1000) at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120) at
com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

Again when I go back to the list and click again the same customer, I do see the default image. I do get this error message on every other clicks. I am totally lost here.

I understand that the placeholder image will be displayed till the image is loaded from url. In this case there is no image in the s3 bucket. There is no way to find out if the image was loaded as I am not using NetworkManager to check for 204 response code. I am directly using s3 url with URLImage.

Any help will be appreciated.

Thank you.

1

1 Answers

0
votes

Clear your local storage to make sure the image is downloaded. Open the network monitor and look at the result. 306 bytes seems like the wrong size for an image so I'm guessing you are getting an error message from amazon and trying to read that as an image.

If you open the network monitor you should be able to inspect the request and response sent and see why the request failed.