Monday, February 24, 2014

Network picture read errors. Great God seeking answers


Do not talk nonsense directly on the source


public class MainActivity extends Activity {
private Handler mHandler;
Bitmap bmImg;
ImageView imView;
String imageUrl = "http://hiphotos.baidu.com/baidu/pic/item/7d8aebfebf3f9e125c6008d8.jpg";

@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
new Thread () {
@ Override
public void run () {
/ / TODO Auto-generated method stub
a.sendEmptyMessage (0x111);
}
.} Start ();
}

Handler a = new Handler () {
@ Override
public void handleMessage (Message msg) {
/ / TODO Auto-generated method stub
if (msg.what == 0x111) {
imView = (ImageView) findViewById (R.id.imview);
imView.setImageBitmap (returnBitMap (imageUrl));
}
}
};

public Bitmap returnBitMap (String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL (url);
} Catch (MalformedURLException e) {
e.printStackTrace ();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
. OpenConnection ();
conn.setDoInput (true);
conn.connect ();
InputStream is = conn.getInputStream ();
bitmap = BitmapFactory.decodeStream (is);
is.close ();
} Catch (IOException e) {
e.printStackTrace ();
}
return bitmap;
}
}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Plus a
conn.disconnect ();
Reply:
 

new Thread () {
@ Override
public void run () {
a.sendEmptyMessage (0x111); / / this place removed
returnBitMap (imageUrl); access to the network should be put code into the picture here, the network needs to put something inside the thread.
}
.} Start ();
returnBitMap method to get inside if it sends a message to the Bitmap images
Inside with
Message msg = Message ();
msg.what = 0x111;
msg.obj = bitmap ;/ / get the picture
a.sendEmptyMessage (msg);

Handler a = new Handler () {
@ Override
public void handleMessage (Message msg) {
/ / TODO Auto-generated method stub
if (msg.what == 0x111) {
imView = (ImageView) findViewById (R.id.imview);
imView.setImageBitmap ((Bitmap) msg.obj);
}
}
};


Reply:
Your code is not wrong, it is not with the main thread to perform network operations

No comments:

Post a Comment