Android学习笔记(十八):ImageView设置网络图片

  • 方案一:读取网络图片的inputStream,然后传给bitmap,再显示在imageView中,但是实际操作的时候,提示错误:android.os.NetworkOnMainThreadException(是因为现在网络请求不允许在主线程main中执行,要另外开辟一个子线程进行)
    下面是出错的将网络图片转成bitmap的方法
//将网络图片转换成bitmap
    public Bitmap getHttpBitmap(String url) {
        URL httpUrl = null;
        Bitmap bitmap = null;
        try {
            httpUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
            conn.setConnectTimeout(6000);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            InputStream in = conn.getInputStream();//这行报错
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this,"系统繁忙,请稍后再试",Toast.LENGTH_SHORT).show();
        }
        return bitmap;

    }

下面是修改后的方法:直接开辟一个子线程,然后在里面设置网络图片,并在onCreate中调用该方法即可

public void setNetworkBitmap() {
        Runnable networkImg = new Runnable() {
            @Override
            public void run() {
                try {
                    URL httpUrl = new URL(getIntent().getStringExtra("QRCode"));
                    HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
                    conn.setConnectTimeout(6000);
                    conn.setDoInput(true);
                    conn.setUseCaches(false);
                    InputStream in = conn.getInputStream();
                    bitmap = BitmapFactory.decodeStream(in);
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        new Thread(networkImg).start();
        while(bitmap == null)
            continue;
        QRImg.setImageBitmap(bitmap);
    }

或者下面的代码也可以,更简洁点

public void setNetworkBitmap() {
        Runnable networkImg = new Runnable() {
            @Override
            public void run() {
                try {
                    URL conn = new URL(getIntent().getStringExtra("QRCode"));
                    InputStream in = conn.openConnection().getInputStream();
                    bitmap = BitmapFactory.decodeStream(in);
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        new Thread(networkImg).start();
        while(bitmap == null)
            continue;
        QRImg.setImageBitmap(bitmap);
    }
  • 方案二:摘自stackoverflow
    http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }

然后在oncreate中执行下面的代码

new DownloadImageTask(QRImg).execute(getIntent().getStringExtra("QRCode"));

你可能感兴趣的:(Android学习笔记)