0
我正在加載來自Twitter供稿的位圖。我使用下面的代碼Bitmap notloading in Imageview
private class getBitmapFromLink extends AsyncTask<String, Void, Bitmap> {
private ImageView imgView;
public getBitmapFromLink(ImageView imgView) {
this.imgView = imgView;
}
@Override
protected Bitmap doInBackground(String... params) {
Bitmap myBitmap;
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
Log.v("BITMAP", e.getMessage());
return null;
} catch (Exception e) {
Log.v("BITMAP", e.getMessage());
return null;
}
}
@Override
protected void onPostExecute(Bitmap result) {
imgView.setImageBitmap(result);
imgView.invalidate();
}
}
當調試myBitmap = BitmapFactory.decodeStream(input);
線直接來到catch塊,並在返回零線。在調試時,我嘗試在日誌上打印錯誤消息,但它不執行該行並直接返回語句。
由於提前
是網址有效嗎?你爲什麼不使用畢加索或壁畫來處理圖片? – Blackbelt
是的。 url有效 - http://pbs.twimg.com/media/CWgUjyHUAAA7eZ3.jpg –
使用picasso從url加載圖片,你可以在這裏查看示例http://stackoverflow.com/questions/20181491/use-picasso-to -get-a-callback-with-a-bitmap –