Prashant Rahate
Prashant Rahate

Reputation: 163

How to convert Buffered Image to Image while using URLImage in [CodenameOne]

with the below code am getting java.lang.ClassCastException: java.awt.image.BufferedImage cannot be cast to com.codename1.ui.Image error. how can i solve this ?

EncodedImage encImage = (EncodedImage) fetchResourceFile().getImage("user2.png");
Image profileImage= URLImage.createToStorage(encImage, "Medium_me.jpg", profile.getProfileImageURL(),URLImage.RESIZE_SCALE);         
System.out.println("Imaged:" + profileImage.getImage());

findPlayerImageGs(f).setIcon((Image) profileImage.getImage()); //Error Line
findPlayerImageGs(f).repaint();

findPlayerImageGs is Label.

Upvotes: 1

Views: 258

Answers (1)

tizbn
tizbn

Reputation: 1907

check the following modified code and it should work

EncodedImage encImage = (EncodedImage)      
fetchResourceFile().getImage("user2.png");
Image profileImage= URLImage.createToStorage(encImage, "Medium_me.jpg",      profile.getProfileImageURL(),URLImage.RESIZE_SCALE);         
System.out.println("Imaged:" + profileImage.getImage());
**findPlayerImageGs(f).setIcon(profileImage); //it should work**
findPlayerImageGs(f).repaint();

Notice that you shouldn't manipulate URLImage and always add it "as is" otherwise it might fail as explained here.

The getImage() method of image returns a platform native version of the image. This is mostly useless to you as a developer as the platform native image differs in each platform, in JavaSE or the simulator image is implemented via AWT's BufferedImage. On iOS you would get a long[] pointing at the memory area where the native Objective-C image is held.

This method exists so native code can get access to a platform dependent handle.

Upvotes: 1

Related Questions