Saturday, March 8, 2014
Android download pictures on a network problem
I'm doing a picture on the network to download the program.
I use Handler to open a sub-thread to perform a read operation picture.
handler.post (new Runnable () {
@ Override
public void run () {
/ / Here to download data
try {
URL url = new URL (params);
HttpURLConnection conn = (HttpURLConnection) url.openConnection ();
conn.setDoInput (true);
conn.connect ();
InputStream inputStream = conn.getInputStream ();
bitmap = BitmapFactory.decodeStream (inputStream);
Message msg = new Message ();
msg.what = 1;
handler.sendMessage (msg);
} Catch (MalformedURLException e1) {
e1.printStackTrace ();
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
}
});
This program is run to connect one on the error.
logcat there NetworkOnMainThreadException.
I obviously was run in sub-thread, how would report this error?
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
post (runnable) just to execute an asynchronous method, this method is still in the main thread of execution, and not open the thread,
You should new Thread (new runnable) start ().;
Reply:
You use handler.post is the main thread, the default is the main thread's message queue, not the child thread.
new Thread () {
public void run () {
}
.} Start ();
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment