Search code examples
codenameone

Open gallery and display selected picture back Codename One


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() {
    }
}

Solution

  • 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.