0

所以我正在编写一个小程序来显示来自 J2me 设备的 URL 的图像。这是我的代码:

//Return Image from URL
public Image loadImage(String url) throws IOException {
        HttpConnection hpc = null;
        DataInputStream dis = null;
        try {
            hpc = (HttpConnection) Connector.open(url);
            int length = (int) hpc.getLength();
            if (length < 0) {
                stringItem = new StringItem("ko dc", url);
                form.append(stringItem);
                return null;
            } else {
                byte[] data = new byte[length];
                dis = new DataInputStream(hpc.openInputStream());
                dis.readFully(data);
                return Image.createImage(data, 0, data.length);
            }
        } finally {
            if (hpc != null) {
                hpc.close();
            }
            if (dis != null) {
                dis.close();
            }
        }
    }

//Display Image from URL
public void run(String x) {
        try {
            String URL = x;
            Image image = loadImage(URL);
            mItem = new ImageItem(null, image, 0, null);

        } catch (IOException ioe) {
            mItem = new StringItem(null, ioe.toString());
        }
        Display display = getDisplay();
        form.append(mItem);

        display.setCurrent(form);
    }

//Return String[] from text file
    public String[] readText(String x) throws IOException {
        InputStream file = getClass().getResourceAsStream(x);
        DataInputStream in = new DataInputStream(file);

        StringBuffer line = new StringBuffer();
        Vector lines = new Vector();

        int c;
        try {
            while ((c = in.read()) != -1) {
                if ((char) c == '\n') {
                    if (line.length() > 0) {
                        // debug
                        //System.out.println(line.toString());
                        String bufferContent = line.toString();
                        lines.addElement(bufferContent);
                        line.setLength(0);
                    }
                } else {
                    line.append((char) c);
                }
            }
            if (line.length() > 0) {
                String bufferContent = line.toString();
                lines.addElement(bufferContent);
                line.setLength(0);
            }

            String[] splitArray = new String[lines.size()];
            for (int i = 0; i < splitArray.length; i++) {
                splitArray[i] = lines.elementAt(i).toString();
            }
            return splitArray;

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        } finally {
            in.close();
        }
    }

这些方法效果很好。但如果我打电话:

String[] y = readText("/p1.txt"); //p1 is a normal txt file. It loads ok.
run(y[0]); //y[0] == "https://lh5.googleusercontent.com/-agMn71xzI5Q/UclQt390CJI/AAAAAAAABQs/wI64cCA9ucs/s800/1%252520%2525282513%252529.jpg", I check it carefully.
// or
//String xTemp = y[0];
//run(xTemp);

它在 loadImage() void 中返回“负数组大小”(我用 if (length < 0) {.. 阻止它)

但如果我打电话:

run("https://lh5.googleusercontent.com/-agMn71xzI5Q/UclQt390CJI/AAAAAAAABQs/wI64cCA9ucs/s800/1%252520%2525282513%252529.jpg";
//or
//String x ="https://lh5.googleusercontent.com/-agMn71xzI5Q/UclQt390CJI/AAAAAAAABQs/wI64cCA9ucs/s800/1%252520%2525282513%252529.jpg";
//run(x);

没关系。我不知道在字符串数组中存储 URL 时会导致错误吗?请你帮助我好吗?谢谢!

4

1 回答 1

1

由于您从文本文件中读取行,因此您必须删除 y[i] 中的最后一个字符(“\n”字符)

x2 = y[i].substring(0, y[i].length()-1);
于 2013-06-28T10:10:20.800 回答