1

我正在尝试显示我的图像,但它没有显示:

public Bitmap getUserPic(String picID) {
    String imageURL;
    Bitmap bitmap = null;
    Log.d("BITMAP", "Loading Picture");
    imageURL = picID;
    Log.d("image url", picID);
    try {
        bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
    } catch (Exception e) {
        Log.d("BITMAP", "Loading Picture FAILED");
        e.printStackTrace();
    }
    return bitmap;
}

Logcat 输出:(如何更整洁地显示我的 logcat 输出?)

10-19 17:01:50.835: D/image url(8636): http://sphotos-c.ak.fbcdn.net/hphotos-ak-prn1/c0.0.403.403/p403x403/554229_4774210003216_2100715293_n.jpg
10-19 17:01:50.840: D/BITMAP(8636): Loading Picture FAILED 10-19
17:01:50.840: W/System.err(8636): android.os.NetworkOnMainThreadException 10-19 17:01:50.840: W/System.err(8636): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
10-19 17:01:50.840: W/System.err(8636): at java.net.InetAddress.lookupHostByName(InetAddress.java:391) 10-19
17:01:50.840: W/System.err(8636): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242) 10-19
17:01:50.840: W/System.err(8636): at java.net.InetAddress.getAllByName(InetAddress.java:220) 10-19
17:01:50.840: W/System.err(8636): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71) 10-19
17:01:50.840: W/System.err(8636): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 10-19
17:01:50.840: W/System.err(8636): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-19 17:01:50.840: W/System.err(8636):     at
libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-19 17:01:50.840: W/System.err(8636):     at
libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 10-19
17:01:50.840: W/System.err(8636):   at
libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-19 17:01:50.840: W/System.err(8636):     at
libcore.net.http.HttpEngine.connect(HttpEngine.java:303) 10-19
17:01:50.840: W/System.err(8636):   at
libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-19 17:01:50.840: W/System.err(8636):     at
libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232) 10-19
17:01:50.840: W/System.err(8636):   at
libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-19 17:01:50.840: W/System.err(8636):     at
java.net.URLConnection.getContent(URLConnection.java:194) 10-19
17:01:50.840: W/System.err(8636):   at
java.net.URL.getContent(URL.java:447) 10-19 
17:01:50.840: W/System.err(8636): at jp.gr.java_conf.akabeko.testimagegallery.MainActivity.getUserPic(MainActivity.java:275)
10-19 17:01:50.845: W/System.err(8636): at jp.gr.java_conf.akabeko.testimagegallery.MainActivity$fetchArticles.onPostExecute(MainActivity.java:251)
10-19 17:01:50.845: W/System.err(8636): at jp.gr.java_conf.akabeko.testimagegallery.MainActivity$fetchArticles.onPostExecute(MainActivity.java:1)
10-19 17:01:50.845: W/System.err(8636): at android.os.AsyncTask.finish(AsyncTask.java:602) 10-19 17:01:50.845:
W/System.err(8636): at android.os.AsyncTask.access$600(AsyncTask.java:156) 10-19
17:01:50.845: W/System.err(8636): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
10-19 17:01:50.845: W/System.err(8636): at android.os.Handler.dispatchMessage(Handler.java:99) 10-19
17:01:50.845: W/System.err(8636): at android.os.Looper.loop(Looper.java:137) 10-19 
17:01:50.845: W/System.err(8636): at android.app.ActivityThread.main(ActivityThread.java:4517) 10-19
17:01:50.845: W/System.err(8636): at java.lang.reflect.Method.invokeNative(Native Method) 10-19
17:01:50.845: W/System.err(8636): at java.lang.reflect.Method.invoke(Method.java:511) 10-19 
17:01:50.845: W/System.err(8636): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:995)
10-19 17:01:50.845: W/System.err(8636): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 10-19
17:01:50.845: W/System.err(8636): at dalvik.system.NativeStart.main(Native Method) 10-19 17:01:50.915: D/CLIPBOARD(8636): Hide Clipboard dialog at Starting input: finished by someone else... !
4

2 回答 2

2

当应用程序尝试在其主线程上执行网络操作时会引发异常。仅针对面向 Honeycomb SDK 或更高版本的应用程序会引发此异常。允许以早期 SDK 版本为目标的应用程序在其主事件循环线程上进行网络连接,但非常不鼓励这样做。

使用 AsyncTask 在单独的线程上获取图像,然后将其分配给在 onPostExecute 和 Voila 上显示!它应该工作。

请参阅此异步任务:http ://android-developers.blogspot.in/2009/05/painless-threading.html

有一个肮脏的修复,但我建议永远不要使用它

 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy); 
于 2012-10-19T09:20:12.213 回答
0

试试这个:

AQuery aq = new AQuery(getActivity());
        aq.id(view.findViewById(R.id.image)).image(imageUrl, true, true, 0,  0,
                new BitmapAjaxCallback() {
                    @Override
                    public void callback(String url, ImageView iv, Bitmap bm, AjaxStatus status){
                        iv.setImageBitmap(bm);
                    }
                }.header("User-Agent", "android"));
于 2013-08-28T05:57:45.340 回答