// Write image to image view
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.getMessage();
return null;
}
}
在这里,当单击按钮时(在Oncreate事件中)我将图像显示到图像视图:
Button picturebutton = (Button) findViewById(R.id.btngetpic);
final ImageView myview = (ImageView) findViewById(R.id.imgview1);
assert picturebutton != null;
picturebutton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
TextView mytext = (TextView) findViewById(R.id.mytext);
mytext.setText("done");
myview.setImageBitmap(getBitmapFromURL("http://169.254.XXX.XXX/Images/Itworks.png"));
}
}
);
我的使用清单中有INTERNET,但每当我运行应用程序时,它都会强行关闭。我觉得它与我的听众有关但我无法解决它。任何帮助表示赞赏,因为我非常困惑。 (我仍然是Android开发的新手,虽然我有java桌面经验)
编辑:我关闭Force的异常堆栈
07-13 18:14:00.718 4077-4077/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.neil.access_control, PID: 4077
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
at libcore.io.IoBridge.connect(IoBridge.java:122)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
at java.net.Socket.connect(Socket.java:884)
at com.android.okhttp.internal.Platform.connectSocket(Platform.java:117)
at com.android.okhttp.internal.http.SocketConnector.connectRawSocket(SocketConnector.java:160)
at com.android.okhttp.internal.http.SocketConnector.connectCleartext(SocketConnector.java:67)
at com.android.okhttp.Connection.connect(Connection.java:152)
at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:185)
at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:341)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
at com.example.neil.access_control.MainActivity.getBitmapFromURL(MainActivity.java:71)
at com.example.neil.access_control.MainActivity$1.onClick(MainActivity.java:54)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-13 18:14:00.727 1283-3180 /? W / ActivityManager:强制完成活动com.example.neil.access_control / .MainActivity
答案 0 :(得分:1)
我知道这是一个替代品"解决你的问题,但我想发布它,因为当我遇到它试图解决我的一系列问题时,它也对我有帮助。
我建议您使用 Picasso 作为图片加载器,因为它可以从网址加载图片,并且还提供以下内容:
就像
一样简单Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
另一方面,只加载位图,你可以做类似的事情(如this SO answer中所述):
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
/* Do something with the bitmap*/
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
/* handle failure */
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
/* you may execute some logic before loading*/
}
};
Picasso.with(context).load(imageURL).into(target);
注意:我与Picasso没有任何关联
答案 1 :(得分:1)
您的问题是:NetworkOnMainThreadException
,您可以read the documentation here,但重要的是Android不能在主线程(图形用户界面)上进行“阻止操作”。
使用AsynkTask或其他线程在互联网/网络上查询数据。
虽然Picasso是一个很好的“图像库”,但我也建议Volley(来自Google),它有一个优秀的文档,以及几个例子。不仅如此,Volley还将“微观管理”最佳库以使用设备上可用的网络。)
请注意,API 19 KitKat上的HttpURLConnection已弃用IIRC