将一个图片的url地址转换为一个bitmap图片


转载
URL url = new URL(path);
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
   conn.setConnectTimeout(5000);
   int max = conn.getContentLength();
   InputStream is = conn.getInputStream();
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   byte[] buffer = new byte[1024];
   int len;
 
   while ((len = is.read(buffer)) != -1) {
    baos.write(buffer, 0, len);
   }
   byte[] result = baos.toByteArray();
   return BitmapFactory.decodeByteArray(result, 0, result.length);
  } catch (Exception e) {
   e.printStackTrace();
   return null;
  }

你可能感兴趣的:(android)