如何从网站获取特定图像并将其显示在Android应用程序中?

时间:2016-09-09 00:04:34

标签: android image url web extract

鉴于网站的网址,我想检索网站的徽标和背景图片,并将其显示在Android应用中。例如,从this link我想要背景图片和黑客西部徽标。

编辑:我忘了提到我试图在运行时间之前为一个未确定大小的URL列表实现此效果

1 个答案:

答案 0 :(得分:0)

使用此AsyncTaskURL

加载图片
 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);