我試圖讓應用程序顯示來自URL的圖像,我相當確定問題是與AsyncTask但我已經返回在過去一週內多次對此代碼進行編碼,但仍然無法確定我要出錯的地方。Android - 從網址抓取圖像並使用AsyncTask在ImageView中顯示它
Internet權限設置,我沒有得到的logcat
ImageView eventImage2;
eventImage2 = (ImageView) findViewById(R.id.eventImage2);
new imageupdate().execute();
public class imageupdate extends AsyncTask<Bitmap, Void, Bitmap> {
@Override
protected Bitmap doInBackground(Bitmap... url) {
URL url1;
try {
url1 = new URL("http://masterzangetsu.eu/Apps/NowIGetYou/banner.png");
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
InputStream is = connection.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img;
}
protected void onPreExecute(String result) {
}
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
eventImage2.setImageBitmap(result);
}
}
至於我可以告訴與
img = BitmapFactory.decodeStream(is);
心不是定義的IMG變量被鏈接到返回
變量return img;
這兩個變量的結果和img都返回爲空
您將'result'設置爲'null',然後使用'img = result' ...這意味着'img'爲'null'。你是否想要轉換'img'和'result'? – codeMagic 2013-04-09 12:58:54
您是否嘗試調試此代碼,查看它出錯的位置? – Egor 2013-04-09 12:59:08
@codeMagic是...這是問題.. – Pragnani 2013-04-09 12:59:42