3

IDE:

  • Eclipse
  • Desktop Windows 7
  • Simulator Nexus 5
  • Device

I want to open the gallery of the device and display the image selected bythe user back. I make a button and and an ActionListener should deflect me to the device's gallery. But the simulator shows a blank screen even if I omit the opening of the gallery part and just add the button. Also, It gives the following exception in the log:-

Jul 20, 2017 4:11:00 PM java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.
java.io.UTFDataFormatException: malformed input around byte 64
 at java.io.DataInputStream.readUTF(Unknown Source)
 at java.io.DataInputStream.readUTF(Unknown Source)
 at com.codename1.ui.util.Resources.loadTheme(Resources.java:1270)
 at com.codename1.ui.util.Resources.openFileImpl(Resources.java:303)
 at com.codename1.ui.util.Resources.openFile(Resources.java:269)
 at com.codename1.ui.util.Resources.<init>(Resources.java:189)
 at com.codename1.ui.util.Resources.open(Resources.java:768)
 at com.codename1.ui.util.Resources.open(Resources.java:688)
 at com.codename1.impl.javase.JavaSEPort$4.run(JavaSEPort.java:1720)
 at com.codename1.ui.Display.processSerialCalls(Display.java:1056)
 at com.codename1.ui.Display.mainEDTLoop(Display.java:873)
 at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
 at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

The following is my main java file made on an empty new bare bone project:-

package com.mycompany.myapp;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Image;
import com.codename1.ui.Button;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.components.ImageViewer;
import com.codename1.io.Log;
import com.codename1.media.MediaManager;
import com.codename1.ui.Toolbar;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import java.io.IOException;
/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class MyApplication {
    private Form current;
    private Resources theme;
    public void init(Object context) {
        theme = UIManager.initFirstTheme("/theme");
        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);
        // Pro only feature, uncomment if you have a pro subscription
        // Log.bindCrashProtection(true);
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World");
        hi.addComponent(new Label("Hi World"));
        Button gallery = new Button("Browse");
       hi.add(gallery);
       gallery.addActionListener(new ActionListener<ActionEvent>() {
 @Override
 public void actionPerformed(ActionEvent evt) {
 // TODO Auto-generated method stub
 Display.getInstance().openGallery((e) -> {
            if(e != null && e.getSource() != null) {
//                String file = (String)e.getSource();
//                try {
//                Label path = new Label(file);
//                hi.add(path);
//                    
//                } catch(Exception err) {
//                    Log.e(err);
//                } 
            }
        }, Display.GALLERY_IMAGE);
 }
 });
    }
    public void stop() {
        current = Display.getInstance().getCurrent();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = Display.getInstance().getCurrent();
        }
    }
    
    public void destroy() {
    }
}
peterh
  • 11,875
  • 18
  • 85
  • 108
Gurankas
  • 229
  • 4
  • 16
  • Solved the first part. I can use the button and am able to write the file path onto the original screen. How can i display the image which I selected though? – Gurankas Jul 20 '17 at 12:07
  • Image image = URLImage.createImage((String)e.getSource()); hi.add(image); <-- Is there a more efficient way to do it? It takes time to get uploaded – Gurankas Jul 20 '17 at 13:12
  • You can use the ImageIO class https://www.codenameone.com/javadoc/com/codename1/ui/util/ImageIO.html – Chen Jul 21 '17 at 04:40

1 Answers1

2

It seems you have several issues. The first is a preferences issue which you should fix with your JDK install:

Resolving the problem The work around is to login as the administrator and create the key HKEY_LOCAL_MACHINE\Software\JavaSoft\Prefs

From Java: java.util.Preferences Failing

Next it seems your resource file is corrupted. It's hard to tell what went wrong there... Is it 0 length?

Check out this article for tracking designer tool issues.

A large image from the camera will take some time to process on some devices. You can capture a smaller image (see the various capture methods) but I'm guessing that you just didn't revalidate after adding to the ui and incorrectly assumed this is slow.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • Thanks for the answer @Shai. Running as administrator helped solve the first error. I was able to find both designer_1.jar and guibuilder_1.jar in the home directory too so I'm not sure what's the problem. Currently, I have two dilemnas:- I get a UTFDataFormatException whenever I run my app and secondly How do I click a Photo from camera and display it on screen? – Gurankas Jul 21 '17 at 05:30
  • I answered a different question you had. This exception is serious... Something seems to be seriously broken with your install – Shai Almog Jul 22 '17 at 04:23
  • @ShaiAlmog Note that this guy was having a problem "around byte 64", just like the other one and me. I've created a couple of projects, all of them reporting the same position. The file is non-empty, `Form.transparency` and `Title.padding` can be read before the exception happens. Created a new project "Phoenix", `@hasRaisedButtonBool` and `Title.padding` were read, followed by "malformed input around byte 64" again thrown when reading a string with of length 16256 = 63 * 256 + 128. Different `theme.res`, same problem. No designer used, it was a freshly auto-created demo. – maaartinus Aug 22 '17 at 04:06
  • Go to Codename One Settings -> Basic -> Update Client Libs and see if this issue continues after that. – Shai Almog Aug 23 '17 at 05:15