鉴于网站的网址,我想检索网站的徽标和背景图片,并将其显示在Android应用中。例如,从this link我想要背景图片和黑客西部徽标。
编辑:我忘了提到我试图在运行时间之前为一个未确定大小的URL列表实现此效果
答案 0 :(得分:0)
使用此AsyncTask
从URL
public class LoadImageTask extends AsyncTask<Void, Void, Void>{
private Bitmap mMyBitmap;
@Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL("https://hackwestern.com/images/logo_vert_spons.png");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
mMyBitmap = BitmapFactory.decodeStream(input);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
mImageView.setImageBitmap(mMyBitmap);
}
}
然后在OnCreate
或您需要的地方调用它:
new LoadImageTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);