1

我正在从 url 检索图像并将它们显示在imageview. 但我的代码给了我空消息。

ImageView imageview = (ImageView) findViewById(R.id.imageView1);
try {
    Uri uri=Uri.parse("http://10.0.2.2:8083/call/rahul.jpg");
    // uri.buildUpon().appendQueryParameter("key", "Android developer");
    URI u = new URI(uri.toString());
    System.out.println("path is "+u);
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet();
    httpget.setURI(u);
    //httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    is = entity.getContent(); 

    Bitmap bitmap = BitmapFactory.decodeStream(is);
    imageview.setImageBitmap(bitmap); 
} catch(Exception e) {
    System.out.println("this is the error "+e.getMessage()); 
}

日志猫:-

02-04 19:32:35.355: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:35.355: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
02-04 19:32:35.895: I/dalvikvm(679): threadid=3: reacting to signal 3
02-04 19:32:35.915: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:35.915: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
02-04 19:32:36.425: I/dalvikvm(679): threadid=3: reacting to signal 3
02-04 19:32:36.445: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:36.445: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
02-04 19:32:36.992: I/dalvikvm(679): threadid=3: reacting to signal 3
02-04 19:32:37.005: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:37.005: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
02-04 19:32:37.565: I/dalvikvm(679): threadid=3: reacting to signal 3
02-04 19:32:37.585: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:37.585: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
02-04 19:32:38.105: I/dalvikvm(679): threadid=3: reacting to signal 3
02-04 19:32:38.125: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:38.125: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
02-04 19:32:38.645: I/dalvikvm(679): threadid=3: reacting to signal 3
02-04 19:32:38.665: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:38.665: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
02-04 19:32:39.175: I/dalvikvm(679): threadid=3: reacting to signal 3
02-04 19:32:39.195: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:39.205: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
02-04 19:32:39.695: I/dalvikvm(679): threadid=3: reacting to signal 3
02-04 19:32:39.716: D/dalvikvm(679): threadid=1: still suspended after undo (sc=1 dc=1)
02-04 19:32:39.716: I/dalvikvm(679): Wrote stack traces to '/data/anr/traces.txt'
4

2 回答 2

0

尝试这个:

ImageView imageview = (ImageView) findViewById(R.id.imageView1);
mBitmap = BitmapFactory.decodeStream((InputStream) new URL(http://10.0.2.2:8083/call/rahul.jpg).getContent());
imageview .setImageBitmap(mBitmap);
于 2013-02-04T14:14:33.437 回答
0

好吧,既然您说我的建议对您很有效,我想我应该将其作为答案提交。我观察到您似乎距离一个可行的解决方案还有很长的路要走,而且由于在您可能希望使用诸如android-query之类的库之前,这个问题已经解决了很多次。我与那个图书馆没有任何关系——我以前自己也用过。另外,由于我没有从技术上回答你的问题,虽然我解决了你的问题,但我最好为绿色勾号多做一点,嗯?

您对库的使用看起来不错,并且您不需要调用回收(顺便说一句,您可以在 Google 上轻松找到)。请注意,您也可以用它做很多其他的好事情,例如在加载图像时淡入淡出,如果它们非常大,则在加载时对其进行下采样等等。您可以在 android-query 网站上找到很多示例。

至于您对它是否免费的询问,许可证是在我链接到的主页上声明的,在靠近顶部的左侧。它是Apache 许可证 2.0。简而言之,此许可证允许您自由使用该库,只要您在您的应用程序的任何发行版中包含 Apache 许可证的副本并明确提及您在该许可证下使用的任何库。或者您可以阅读他们的常见问题解答中的简介。您通常会在您的应用程序中为此类事情放置一个 about 屏幕,这清楚地表明您正在使用 Apache 2.0 许可证下的 android-query 库,并带有可查看的许可证副本。正确的归属:我想你会同意,在这个许可下你可以找到这么多有用的东西,这是一个很小的代价:o)

祝你的应用好运!

于 2013-02-05T18:59:34.120 回答