Anas
Anas

Reputation: 177

How to Read Images from storage in codenameone

Can anyone show me how to read images .png or .jpg from .cn1 (Stoarge) in codenameOne.

i tried two ways they both don t work :

1- img = EncodedImage.create(Storage.getInstance().createInputStream("img.png"));

2- img = Image.createImage(Storage.getInstance().createInputStream("img.png"));

the errors i get are respectively:

1- Warning: loading large images using EncodedImage.create(InputStream) might lead to memory issues, try using EncodedImage.create(InputStream, int)

--> When i try EncodedImage.create(InputStream, int size) and i execute the function: "findImageViewer().setImage(img);" , i get : java.lang.NullPointerException

2) with "findImageViewerRdM().setImage(img);" i get java.lang.NullPointerException

Upvotes: 2

Views: 467

Answers (2)

tizbn
tizbn

Reputation: 1907

this works for me so please check it .

InputStream is =  Storage.getInstance().createInputStream("tizbn.JPG");

        EncodedImage i = EncodedImage.create(is, is.available());
        ImageViewer imageViewer = new ImageViewer(i);
        f.addComponent(BorderLayout.CENTER, imageViewer);

Upvotes: 4

tizbn
tizbn

Reputation: 1907

img = EncodedImage.create(Storage.getInstance().createInputStream("img.png", int )); Please use the next method with int variable one create(InputStream i, int size) of EncodedImage

Upvotes: 2

Related Questions