我正在尝试从网址加载图片。我找到了一个回答这个问题的堆栈问题,并说要使用这段代码:
try {
ImageView i = (ImageView)findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
但是当我尝试运行它时,我收到了这个错误:
06-19 14:45:02.598: E/AndroidRuntime(10230): FATAL EXCEPTION: main
06-19 14:45:02.598: E/AndroidRuntime(10230): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.beerportfoliopro/com.example.beerportfoliopro.BeerPage}: android.os.NetworkOnMainThreadException
06-19 14:45:02.598: E/AndroidRuntime(10230): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
06-19 14:45:02.598: E/AndroidRuntime(10230): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357)
06-19 14:45:02.598: E/AndroidRuntime(10230): at android.app.ActivityThread.access$600(ActivityThread.java:153)
06-19 14:45:02.598: E/AndroidRuntime(10230): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
06-19 14:45:02.598: E/AndroidRuntime(10230): at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 14:45:02.598: E/AndroidRuntime(10230): at android.os.Looper.loop(Looper.java:137)
06-19 14:45:02.598: E/AndroidRuntime(10230): at android.app.ActivityThread.main(ActivityThread.java:5226)
06-19 14:45:02.598: E/AndroidRuntime(10230): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 14:45:02.598: E/AndroidRuntime(10230): at java.lang.reflect.Method.invoke(Method.java:511)
06-19 14:45:02.598: E/AndroidRuntime(10230): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
06-19 14:45:02.598: E/AndroidRuntime(10230): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
06-19 14:45:02.598: E/AndroidRuntime(10230): at dalvik.system.NativeStart.main(Native Method)
06-19 14:45:02.598: E/AndroidRuntime(10230): Caused by: android.os.NetworkOnMainThreadException
06-19 14:45:02.598: E/AndroidRuntime(10230): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-19 14:45:02.598: E/AndroidRuntime(10230): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-19 14:45:02.598: E/AndroidRuntime(10230): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-19 14:45:02.598: E/AndroidRuntime(10230): at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-19 14:45:02.598: E/AndroidRuntime(10230): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
06-19 14:45:02.598: E/AndroidRuntime(10230): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-19 14:45:02.598: E/AndroidRuntime(10230): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
06-19 14:45:02.598: E/AndroidRuntime(10230): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
06-19 14:45:02.598: E/AndroidRuntime(10230): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-19 14:45:02.598: E/AndroidRuntime(10230): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
06-19 14:45:02.598: E/AndroidRuntime(10230): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
06-19 14:45:02.598: E/AndroidRuntime(10230): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
06-19 14:45:02.598: E/AndroidRuntime(10230): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
06-19 14:45:02.598: E/AndroidRuntime(10230): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
06-19 14:45:02.598: E/AndroidRuntime(10230): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
06-19 14:45:02.598: E/AndroidRuntime(10230): at java.net.URLConnection.getContent(URLConnection.java:190)
06-19 14:45:02.598: E/AndroidRuntime(10230): at libcore.net.http.HttpsURLConnectionImpl.getContent(HttpsURLConnectionImpl.java:175)
06-19 14:45:02.598: E/AndroidRuntime(10230): at java.net.URL.getContent(URL.java:447)
06-19 14:45:02.598: E/AndroidRuntime(10230): at com.example.beerportfoliopro.BeerPage.onCreate(BeerPage.java:52)
06-19 14:45:02.598: E/AndroidRuntime(10230): at android.app.Activity.performCreate(Activity.java:5104)
06-19 14:45:02.598: E/AndroidRuntime(10230): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-19 14:45:02.598: E/AndroidRuntime(10230): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
答案 0 :(得分:2)
你得到NetworkOnMainThreadException
。这是因为从Android 3.0及以上网络操作开始不应该在UI线程中完成,而是在单独的线程中完成。您可以使用AsyncTask或IntentService来实现此目的
换句话说,连接到互联网并下载图像的那段代码应该在AsyncTask
或IntentService
答案 1 :(得分:0)
请尝试以下代码
private Bitmap downloadImage(String imageUrl)
{
Bitmap bitmap=null;
InputStream is;
try
{
is=openHttp(imageUrl);
bitmap=BitmapFactory.decodeStream(is);
is.close();
}
catch (Exception e)
{
// TODO: handle exception
}
return bitmap;
}
private InputStream openHttp(String url) throws IOException
{
InputStream is=null;
int res=-1;
URL u=new URL(url);
URLConnection con=u.openConnection();
if(!(con instanceof HttpURLConnection))
throw new IOException("Not Http Connection");
try {
HttpURLConnection httpCon=(HttpURLConnection)con;
httpCon.setAllowUserInteraction(false);
httpCon.setInstanceFollowRedirects(true);
httpCon.setRequestMethod("GET");
httpCon.connect();
res=httpCon.getResponseCode();
if(res==HttpURLConnection.HTTP_OK)
{
is=httpCon.getInputStream();
}
}
catch (Exception e) {
// TODO: handle exception
}
return is;
}
这里的downloadImage函数重新生成位图对象。 和openHttp返回图片网址的输入流... 检查你的申请......