Problem Description:
Android.os.NetworkOnMainThreadException exception (do not access network resources on the main thread)
Problem Analysis:
Careful study of The reason for the error is that the code does not conform to the Android specification ), online search found: android3.0 version started ( specifically is not starting from this version, do not delve into the ) on the mandatory program can not access the network in the main thread, to put the access network in a separate thread.
How to resolve:
1, If you want to ignore these mandatory policy issues, you can add in the OnCreate () method
Strictmode.setthreadpolicy (New StrictMode.ThreadPolicy.Builder ()
. Detectdiskreads (). Detectdiskwrites (). Detectnetwork (). Penaltylog (). build ());
Strictmode.setvmpolicy (New StrictMode.VmPolicy.Builder ()
. Detectleakedsqlliteobjects (). Detectleakedclosableobjects ()
. Penaltylog (). Penaltydeath (). build ());
2, put in a separate thread: (I use the click button to view the network pictures, learning)
Private Bitmap Bitmap = null;//global variable (if I put it in a local variable, I will be prompted: The immutable local variable cannot be assigned, and the lazy is put into the global variable )
Button Listener:
private final class buttonlistenerevent implements onclicklistener{@Overridepublic void onclick (VIEW V) { final String path = Imagepath.gettext (). toString (); final handler handler = new Handler () { public void handlemessage (message msg) { switch (msg.what) { Case 0: imageview.setimagebitmap ( Bitmap); break; default: break; } }};new thread () { public void&nBsp;run () { try { byte[] data = imageservice.getimage (Path); // to set the bitmap for the image bitmap = bitmapfactory.decodebytearray (data, 0, data.length); // Send an empty message after processing handler.sendemptymessage (0); } catch (exception e) { // TODO Auto-generated catch block e.printstacktrace (); toast.maketext (Getapplicationcontext (), r.string.error, 1) &nBsp; .show (); } } }.start (); }}
I am new to Android, if there is a wrong place also ask you a lot of guidance ~
Android Problem Summary (Android.os.NetworkOnMainThreadException exception)