无法从网址加载图片

时间:2012-06-29 07:06:56

标签: blackberry httpconnection

我无法从网址加载图片并进入我的列表域

ImageLoader上课

public class Util_ImageLoader {
    public static Bitmap getImageFromUrl(String url) {
        Bitmap bitmap = null;

        try {
            String bitmapData = getDataFromUrl(url);
            bitmap = Bitmap.createBitmapFromBytes(bitmapData.getBytes(), 0,
                    bitmapData.length(), 1);
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        return bitmap;
    }

    private static String getDataFromUrl(String url) {
        StringBuffer b = new StringBuffer();
        InputStream is = null;
        HttpConnection c = null;

        long len = 0;
        int ch = 0;

        try {
            c = (HttpConnection) Connector.open(url);

            is = c.openInputStream();
            len = c.getLength();
            if (len != -1) {
                for (int i = 0; i < len; i++)
                    if ((ch = is.read()) != -1) {
                        b.append((char) ch);
                    }
            } else {
                while ((ch = is.read()) != -1) {
                    len = is.available();
                    b.append((char) ch);
                }
            }

            is.close();
            c.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return b.toString();
    }
}

ListField上课

image = new BitmapField(Util_ImageLoader.getImageFromUrl(
    "http://www.orientaldaily.com.my/images/articles/4_APRIL_BLACK_copy.jpg"),
    Field.FIELD_HCENTER | Field.FIELD_VCENTER);
row.add(image);
field = getField(3);
layoutChild(field, 100, 80);
setPositionChild(field, getPreferredWidth() - 105, 5);

2 个答案:

答案 0 :(得分:1)

我相信Alan说他只需要支持OS 5.0及以上版本。如果这是真的,那么我建议他打扰构建连接扩展字符串,例如";interface=wifi"

OS 5.0添加了ConnectionFactory class,这使得这更容易。

BlackBerry设备可以使用多种不同的网络传输之一发出网络请求。一些应用程序关心使用哪些传输。其他应用只需要任何可用的传输。

有关使用ConnectionFactorycreate a Connection with the first available transport, see this example

的示例

有关更高级的示例,该示例显示使用ConnectionFactory指定list of transports to use first, and which to not use at all, see this example

在第二个示例中,代码使用的是BrowserField,Alan没有使用。但是,他可以替换他的代码

c = (HttpConnection) Connector.open(url);

有了这个

c = (HttpConnection) MyConnectionFactory.getConnection(url).getConnection();

其中MyConnectionFactoryshown in the sample code

答案 1 :(得分:0)

您需要为您的网址添加连接扩展名。

如果是wifi然后“; interface = wifi”

Example: c = (HttpConnection) Connector.open(url+";interface=wifi");

使用完美连接参数

c = (HttpConnection) Connector.open(url+getConnParam());

获取连接扩展的代码示例:

public static String getConnParam(){
        String connectionParameters = "";
        if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
        // Connected to a WiFi access point
        connectionParameters = ";interface=wifi";
        } else {
        int coverageStatus = CoverageInfo.getCoverageStatus();
        ServiceRecord record = getWAP2ServiceRecord();
        if (record != null
        && (coverageStatus & CoverageInfo.COVERAGE_DIRECT) ==
        CoverageInfo.COVERAGE_DIRECT) {
        // Have network coverage and a WAP 2.0 service book record
        connectionParameters = ";deviceside=true;ConnectionUID="
        + record.getUid();
        } else if ((coverageStatus & CoverageInfo.COVERAGE_MDS) ==
        CoverageInfo.COVERAGE_MDS) {
        // Have an MDS service book and network coverage
        connectionParameters = ";deviceside=false";
        } else if ((coverageStatus & CoverageInfo.COVERAGE_DIRECT) ==
        CoverageInfo.COVERAGE_DIRECT) {
        // Have network coverage but no WAP 2.0 service book record
        connectionParameters = ";deviceside=true";
        }

    }
        return connectionParameters;
    }

参考网址:

http://www.blackberry.com/developers/docs/4.6.0api/javax/microedition/io/Connector.html#http

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/What_Is_-_Different_ways_to_make_an_HTTP_or_socket_connection.html?nodeid=826935&vernum=0