Android手机开发:从网络上读取图片
Android手机开发:从网络上读取图片
程序:
- /**
- * 根据图片的网络地址url,获取图片
- * 添加权限
- * <uses-permission Android:name="android.permission.INTERNET" />
- */
- public static Bitmap GetNetBitmap(String url) {
- URL imageUrl = null;
- Bitmap bitmap = null;
- try {
- imageUrl = new URL(url);
- }catch(MalformedURLException e) {
- Log.e("MalformedURLException", e.getMessage());
- }
- try {
- HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
- conn.setDoInput(true);
- //将得到的数据转换成InputStream
- InputStream is = conn.getInputStream();
- //将InputStream转换成Bitmap
- bitmap = BitmapFactory.decodeStream(is);
- is.close();
- }catch(IOException e) {
- e.printStackTrace();
- Log.e("IOException", e.getMessage()+"\n");
- }
- return bitmap;
- }
评论暂时关闭