Java代码

String urlpath="http://i2.sinaimg.cn/dy/dsgb/20083.jpg";    try {URL url=new URL(urlpath);HttpURLConnection con = (HttpURLConnection) url.openConnection();con.setConnectTimeout(6000);con.setRequestMethod("GET");if(con.getResponseCode()==200){byte[] imagebytes = readStreamtoBytes(con.getInputStream());File file =new File("pic.jpg");FileOutputStream fos =new FileOutputStream(file);fos.write(imagebytes);fos.close();}

Java代码

public static byte[] readStreamtoBytes(InputStream instream) throws IOException{

ByteArrayOutputStream outstream =new ByteArrayOutputStream();

int len=-1;
byte[] b = new byte[1024];
while((len = instream.read(b)) != -1){

outstream.write(b, 0, len);
}
outstream.flush();
outstream.close();
instream.close();

return outstream.toByteArray();

}

更多相关文章

  1. 在Eclipse 中关联android sdk 源代码
  2. 操作内存卡的常用代码
  3. android 根据SD卡中图片路径读取并显示SD中的图片——源代码
  4. android代码混淆及打包相关
  5. android 中生成XML文件的代码
  6. [置顶] [Android自定义控件]Android中如何用代码写出优美的Button
  7. recovery代码流程
  8. Android-开发常用代码汇总

随机推荐

  1. Android Handler 主线程 子线程 双向通信
  2. Android之SurfaceView简介(一)
  3. Android 之多线程下载原理
  4. Android实现推送方式解决方案 (转1)
  5. [Android Studio系列(三)]Android Studio
  6. Android ListView 图片异步加载和图片内
  7. 自定义实现类似android主界面的滑屏换屏
  8. Android天线信号刷新流程
  9. android、windows上多个USB Camera同时使
  10. ubuntu12.04 安装android sdk /ndk/源码