我正在開發一個android應用程序,其中我從服務器加載圖像,但是當我這樣做時,android應用程序中沒有圖像(顯示該部分空白)。有人可以告訴我我在做什麼錯?這裏是我正在執行的代碼:從網絡加載圖片時出現問題
public class ad extends Activity {
ImageView image_view;
final static String imageLocation="http://example.com/ads/banner320.png";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.select);
image_view = (ImageView)findViewById(R.id.imageView1);
loadImage(imageLocation);
}
Bitmap bitmap;
void loadImage(String image_location){
URL imageURL = null;
try {
imageURL = new URL(image_location);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection connection= (HttpURLConnection)imageURL.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap
image_view.setImageBitmap(bitmap);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
請幫忙通過在此代碼中提出一些更改或錯誤我在做什麼?