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 で "Negative Array Size" を返します (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 に答える