在 Java J2ME 中加载图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1332205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:08:52  来源:igfitidea点击:

Loading image in Java J2ME

javajava-memidplcdui

提问by Sopolin

I have a problem with loading image with java 2ME. I have a image file "picture.png" in location drive "C:". After that I wrote my like this to show image from this location.

我在使用 java 2ME 加载图像时遇到问题。我在位置驱动器“C:”中有一个图像文件“picture.png”。在那之后,我写了我这样的来显示这个位置的图像。

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;

public class ImageMidlet extends MIDlet implements CommandListener{
    private Display display;
    private Command exitCommand;
    private Command backCommand;
    private Command okCommand;
    private Form form;

    private ImageItem imageItem;
    private Image image;

    public ImageMidlet(){
        display = Display.getDisplay(this);
        form=new Form("");
        exitCommand = new Command("Exit", Command.EXIT, 1);
        backCommand = new Command("Back", Command.BACK, 2);
        okCommand = new Command("OK", Command.OK, 3);

        try {
            image=Image.createImage("/picture.png");
            imageItem=new ImageItem(null,image,ImageItem.LAYOUT_NEWLINE_BEFORE,"");
        }
        catch(IOException ex){

        }
        form.append(imageItem);
        form.addCommand(okCommand);
        form.addCommand(exitCommand);
        form.addCommand(backCommand);
        form.setCommandListener(this);

        display.setCurrent(form);

    }

    public void commandAction(Command c,Displayable d){

    }

    public void startApp() {
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}

It shows me this error:

它向我展示了这个错误:

Unable to create MIDlet Test.ImageMidlet
java.lang.NullPointerException
     at javax.microedition.lcdui.Form.append(Form.java:638)
     at Test.ImageMidlet.<init>(ImageMidlet.java:39)
     at java.lang.Class.runCustomCode(+0)
     at com.sun.midp.midlet.MIDletState.createMIDlet(+34)
     at com.sun.midp.midlet.Selector.run(Selector.java:151)

I am starting learn to develop, so please guide to do this.

我开始学习开发,所以请指导这样做。

回答by msell

Image.createImage(String name)loads the given image as a resource. Resources are loaded with Class.getResourceAsStream(name), which looks up the resources from classpath, not from your file system root.

Image.createImage(String name)加载给定的图像作为资源。资源使用Class.getResourceAsStream(name)加载,它从类路径中查找资源,而不是从文件系统根目录中查找资源。

You should put the image file in your classpath, which is usually the final application .jar file. Usually a folder called resourcesor resis created under the project, where the images are placed. The contents of this folder are then copied to the .jar file. In development phase you should be able to append your resource folder to the classpath with a command-line parameter (java -cp resourcesin Java SE) or with a similar IDE setting.

您应该将图像文件放在您的类路径中,这通常是最终的应用程序 .jar 文件。通常在项目下创建一个名为资源资源的文件夹,其中放置图像。然后将此文件夹的内容复制到 .jar 文件中。在开发阶段,您应该能够使用命令行参数(Java SE 中的java -cp 资源)或类似的 IDE 设置将资源文件夹附加到类路径。

If you are really interested in loading the images from actual file system, you can use optional FileConnection API (http://developers.sun.com/mobility/apis/articles/fileconnection/). The handset support for this API is limited though. For static images the classpath is the way to go.

如果您真的对从实际文件系统加载图像感兴趣,您可以使用可选的 FileConnection API ( http://developers.sun.com/mobility/apis/articles/fileconnection/)。不过,手机对这个 API 的支持是有限的。对于静态图像,类路径是要走的路。

回答by JaanusSiim

As msellsaid - You can't access images from Your computer. Make sure that You have included the given image in midlet jar file. If You try to access it using '/picture.png', then it should be located a the root directory in jar.

正如msell所说 - 您无法从您的计算机访问图像。确保您已在 midlet jar 文件中包含给定的图像。如果您尝试使用“/picture.png”访问它,那么它应该位于 jar 中的根目录。

回答by Vikesh

First of all place your image in default package. I have placed "My Network Places.png" in default package. Then create MIDlet named "ImageItemExample" then copy below code in that MIDlet file.

首先将您的图像放在默认包中。我已将“网上邻居.png”放在默认包中。然后创建名为“ImageItemExample”的 MIDlet,然后在该 MIDlet 文件中复制以下代码。

import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ImageItemExample extends MIDlet implements CommandListener{
private Display display;
private Command exit;
private Form form;
private ImageItem logo;

public ImageItemExample(){
form = new Form("Image Item");
exit = new Command("Exit", Command.EXIT, 0);
try{
  logo = new ImageItem(null, Image.createImage("/My Network Places.png"),
   ImageItem.LAYOUT_CENTER | ImageItem.LAYOUT_NEWLINE_BEFORE |
    ImageItem.LAYOUT_NEWLINE_AFTER, "Roseindia");
  form.append(logo);
}catch(IOException e){
  form.append(new StringItem(null, "Roseindia: Image not available: "+ e));
}
}

public void startApp(){
display = Display.getDisplay(this);
form.addCommand(exit);
form.setCommandListener(this);
display.setCurrent(form);
}

public void pauseApp(){}

public void destroyApp(boolean unconditional){
notifyDestroyed();
}

public void commandAction(Command c, Displayable d){
String label = c.getLabel();
if(label.equals("Exit")){
  destroyApp(true);
}
}
}

回答by Prashast

My guess is that

我的猜测是

image=Image.createImage("/picture.png");

throws an exception which prevents the creation of a new object of type ImageItem which leaves your imageItem variable as null. This gives you the null pointer exception.

抛出一个异常,该异常阻止创建 ImageItem 类型的新对象,该对象将您的 imageItem 变量保留为 null。这为您提供了空指针异常。

Isn't your file Picture.pngand not Pictur.png?

你的文件不是Picture.png而不是 Pictur.png 吗?

回答by user128026

Verify that the file picture.pngactually exists

验证文件picture.png确实存在

depending on the device emulator/IDE there should be a way to set the "HOME" directory for the device. In your case, this would be "C:\"

根据设备模拟器/IDE,应该有一种方法可以为设备设置“HOME”目录。在您的情况下,这将是“C:\”