Android 获取链接中的图片

使用以下代码即可获取url对应的图片

public Bitmap getBitMap(String url) { URL myFileUrl = null; Bitmap bitmap = null; try { myFileUrl = new URL(url); } catch (MalformedURLException e) { e.printStackTrace(); } try { HttpURLConnection conn = (HttpURLConnection) myFileUrl .openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); is.close(); } catch (IOException e) { e.printStackTrace(); } return bitmap; }

然后直接使用以下语句即可显示图片:

ImageView imgView = (ImageView) findViewById(R.id.Image);

imgView.setImageBitmap(getBitMap(imgLink));

你可能感兴趣的:(android,String,url)