ImageViewer to view local images question

322 views
Skip to first unread message

piza...@gmail.com

unread,
May 13, 2015, 11:30:28 AM5/13/15
to codenameone...@googlegroups.com
In a Statemachine method with an ImageViewer defined in the Gui builder
I wish to change the image displayed in the image viewer with one loaded from the user storage.
so in the Netbeans Simulator:

String aFilename aFilebrowser.getSelectedFilePath(); // eg "/C:/Users/me/mytestfile.png"
Image codeNameOneImage= EncodedImage.create(aFilename);
ImageViewer myImageViewer=this.findImageViewer();
myImageViewer.setImage( codeNameOneImage);
 myImageViewer.setVisible(true);

gives me the following error:

ERROR: resources must reside in the root directory thus must start with a '/' character in Codename One! Invalid resource:/C:/Users/me/mytestfile.png
java.lang.NullPointerException
    at com.codename1.io.Util.copy(Util.java:116)
    at com.codename1.io.Util.copy(Util.java:102)
    at com.codename1.io.Util.readInputStream(Util.java:169)
    at com.codename1.ui.EncodedImage.create(EncodedImage.java:269)
    at com.codename1.ui.EncodedImage.create(EncodedImage.java:425).......

what am I doing wrong? I want to be able to load an image that the user has saved to the phone from say an email attachement etc.


Shai Almog

unread,
May 13, 2015, 11:55:25 AM5/13/15
to codenameone...@googlegroups.com, piza...@gmail.com, piza...@gmail.com
That's not a valid image path. File paths are file://....
How did you create the aFilebrowser class?

You cannot hardcode a path since device paths differ.

piza...@gmail.com

unread,
May 15, 2015, 9:29:29 AM5/15/15
to codenameone...@googlegroups.com, piza...@gmail.com
Dear Shai
Thanks for replying,
-Filebrowser based on uploadPhoto from FBDemo
- Trying to debug my app first in the simulator running on netbeans
- Used  this.codeNameOneImage= EncodedImage.create("file://C:/Users/Documents/NetBeansProjects/myproject/build/classes/resources/CrunchifyQR.png");
- I understand that I cannot hardcode the file path but I am getting the same error message for either a filepath  starting as /C/...  or file:/C which gives the following error message
java.lang.NullPointerException
ERROR: resources must reside in the root directory thus must start with a '/' character in Codename One! Invalid resource: file://C:/Users/Documents/NetBeansProjects/myproject/build/classes/resources/CrunchifyQR.png

Shai Almog

unread,
May 15, 2015, 12:21:37 PM5/15/15
to codenameone...@googlegroups.com, piza...@gmail.com, piza...@gmail.com
Hi,
that's not a valid path.
Files in the simulator are something like file://home/... and they map to the .cn1 directory to make it "feel" like a device.
You can use the pick from gallery API: http://www.codenameone.com/blog/gallery-icon-update.html

piza...@gmail.com

unread,
May 18, 2015, 11:54:23 AM5/18/15
to codenameone...@googlegroups.com, piza...@gmail.com
Hi Shai
Thanks for helping :)

I think I cannot see the wood for the trees. so I try the example from the gallery Icon Update:
using the code( see below) in the Netbeans Simulator running on Windows.
And I get the same errors.
Questions:
1. In the netbeans project where should I create the .cn directory? would this solve it ?

2. Is there a bug in the simulator that gives only a single trailing "/" for file urls.
   i.e.  (String) ev.getSource(); always gives file:/thefile.png rather than file://thefile.png
  I get the same error for both formulations of the file URL.

3. Running the code under on windows and netbean simulator the picker always gives me the file:/C:/file.png formulation
should I recast this in the test code running on the simulator to file://thefile.png and place it in the .cn directory?
4. I do not understand why it complains about resources when I am trying to load a local device file  and not one from the resources directory


-------------------------------
  Display.getInstance().openGallery(new ActionListener() {
                public void actionPerformed(ActionEvent ev) {
                    if (ev != null && ev.getSource() != null) {
                        String filePathToGalleryImageOrVideo = (String) ev.getSource();
                        System.out.println("filePathToGalleryImageOrVideo   " + filePathToGalleryImageOrVideo);
                        try {
                           Image codeNameOneImage = EncodedImage.create(filePathToGalleryImageOrVideo);
                        } catch (IOException ex) {
                            Logger.getLogger(CodeScan.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }, Display.GALLERY_ALL);
-------------------------------------------------------------------
Output is :
Load form test scenario
filePathToGalleryImageOrVideo    file:/C:/CrunchifyQR.png

ERROR: resources must reside in the root directory thus must start with a '/' character in Codename One!
 Invalid resource: file:/C:/CrunchifyQR.png
[EDT] 0:0:0,0 - java.lang.NullPointerException
[EDT] 0:0:0,2 - java.lang.NullPointerException


java.lang.NullPointerException
    at com.codename1.io.Util.copy(Util.java:116)
    at com.codename1.io.Util.copy(Util.java:102)
    at com.codename1.io.Util.readInputStream(Util.java:169)
    at com.codename1.ui.EncodedImage.create(EncodedImage.java:269)
    at com.codename1.ui.EncodedImage.create(EncodedImage.java:425)
    at com.xxx.xxx.CodeScan$1.actionPerformed(CodeScan.java:92)

Shai Almog

unread,
May 18, 2015, 11:50:56 PM5/18/15
to codenameone...@googlegroups.com, piza...@gmail.com, piza...@gmail.com
Hi,
1. you shouldn't create that directory. Its already created in the home directory of your user.

2. I think there is already an issue for that about the inconsistency of file URL's. Its a bit hard to fix since some developers have coded workarounds into their apps.

3. That's a bug probably, the file in the picker should be openable. I'm not sure if this was fixed or not by now, are your libraries up to date?

piza...@gmail.com

unread,
May 19, 2015, 6:19:08 AM5/19/15
to codenameone...@googlegroups.com, piza...@gmail.com
Ok I narrowed down what I was doing wrong:
I updated my libraries first to make sure then.

When you use EncodedImage.create(filename) or even FileEncodedImage.create(filename)
you get all the headaches mentioned above.

BUT....

if you use Image aImage=FileEncodedImage.create("file://CrunchifyQR.png",1024,1024);
or
Image aImage=FileEncodedImage.create("file://C:/CrunchifyQR.png",1024,1024);
It works if you add the image hieght (i.e. 1024x1024)

I placed the file CrunchifyQR.png in the directory .cn in my home directory
and the path "/CrunchifyQR.png" also worked. Image aImage=FileEncodedImage.create("/CrunchifyQR.png",1024,1024);

The behaviour of the picker was good to discover as it means that once I start testing on real hardware I will
remember to add the steps to check the file path returned.

Shai Almog

unread,
May 19, 2015, 10:20:48 AM5/19/15
to codenameone...@googlegroups.com, piza...@gmail.com, piza...@gmail.com
FileEncodedImage is a pretty different thing.
If you use the EncodedImage factory method that takes sizes the image isn't loaded so you will get no exception. It will only get loaded when you actually need it at which point we can't throw an IOException (for obvious reasons).

Again... You need to "construct" a file path, there is no way around it.
Reply all
Reply to author
Forward
0 new messages