【问题标题】:Unable to load image from url无法从 url 加载图像
【发布时间】:2012-06-30 17:34:03
【问题描述】:

我无法从 url 加载图像并在我的列表字段中播放

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);

【问题讨论】:

  • 你在使用调试器吗?有什么例外吗?您要为哪个最低版本的 BlackBerry OS(OS 5.0+、6.0+ 等)构建此功能?
  • 如何使用调试器?看不到异常,因为已经尝试捕获,我正在使用 7.0 SDK 和最低 1.0,但如果不是我的设备无法安装,我想将其更改为 5.0 SDK
  • 您是否使用 BlackBerry JDE 来构建您的软件?还是带有 BlackBerry 插件的 Eclipse?还是别的什么?

标签: blackberry httpconnection


【解决方案1】:

您需要将连接扩展添加到您的网址。

如果 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

【讨论】:

  • getWAP2ServiceRecord()指的是什么?
【解决方案2】:

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

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

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

使用ConnectionFactory 到create 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();

其中MyConnectionFactory 是shown in the sample code。

【讨论】:

  • 我仍将使用;interface=wifi,因为现在仍在使用 jre 7.0,但是,很快我可能会更改为 5.0,但我无法更改它,尽管我下载了 jre 5.0
  • @AlanLai,对于在 OS 5.0 或更高版本上运行的任何代码,您不需要使用 ;interface=wifi。因此,对于 OS 7.0,您绝对不需要继续使用连接后缀字符串。 ConnectionFactory 是现在推荐的方法。
  • 问题是如果我不使用则无法显示,但应用后可以显示
  • @AlanLai,您使用的是ConnectionFactory,正如我在上面的答案中显示的那样吗?不是Connector.open(url);。
猜你喜欢
  • 2015-07-25
  • 2020-05-13
  • 1970-01-01
  • 2016-12-15
  • 1970-01-01
  • 1970-01-01
  • 2020-03-24
  • 2019-08-20
相关资源
最近更新 更多