使用HTTP协议下载文件

  • 创建一个URL对象
  • 通过URL对象,创建一个HttpURLConnection对象
  • 调用getInputStream()方法得到InputStream对象
  • 从InputStream对象中读取数据
 InputStream in = null;
try {
URL url = new URL(urlstr);
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
in = urlConn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

向SD卡中写入数据

  • 通过Environment.getExternalStorageDirectory()获取存放路径
  • 创建一个outputStream对象,文件路径以参数传入
  • 将InputStream对象中读取的内容写入文件
 File file = new File(Environment.getExternalStorageDirectory(), filename);
OutputStream out=null;
try {
out=new FileOutputStream(file);
byte[] buffer=new byte[4*1024];
while(in.read(buffer)!=-1) {
out.write(buffer);
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}

权限添加

 <!-- 访问网络权限 -->  
<uses-permission android:name="android.permission.INTERNET"
<!-- 在SDCard中创建于删除文件的权限 -->  
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>  
<!-- 往SDCard中写入数据的权限 -->  
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  

最新文章

  1. scala 的内部类
  2. 获得、修改 SQL Server表字段说明
  3. iOS开发中的错误整理,Changing the delegate of a tab bar managed by a tab bar controller is not allowed
  4. Nodejs开源项目推荐
  5. Spark源码系列(二)RDD详解
  6. 转:Windows Socket五种I/O模型
  7. T4自动生成数据库C#实体类学习(1)
  8. PHP内置的字符串处理函数
  9. oracle中行转列、列转行函数
  10. STM32F0xx_SPI读写(Flash)配置详细过程
  11. SQL获取数据库名,表名,列名,说明等信息
  12. storyBoard方式ScrollView的AutoLayout
  13. [转] C语言多维数组与多级指针
  14. tomcat如何路由映射网址
  15. CF1155F Delivery Oligopoly
  16. 使用docker部署SqlServer
  17. Xcode 断点无用,也不打印输出
  18. ORACLE——EXTRACT() 截取日期时间的函数使用
  19. 记一次nginx强制将https请求重定向http
  20. hostnamectl 修改 CentOS7 主机名

热门文章

  1. Android Studio使用百度地图示例BaiduMapsApiASDemo
  2. 深受C/C 程序员欢迎的11款IDE
  3. cell当中的按钮如何获得cell内容
  4. poj 2594 Treasure Exploration (二分匹配)
  5. ng-repeat产生的对象会带有$$hashkey属性处理方法
  6. 如何:对 Windows 窗体控件进行线程安全调用
  7. GridLayout自定义数字键盘(两个EditText)
  8. css3新特性@media(媒体查询)
  9. Oracle查询某段日期内某个时间段的数据
  10. POJ 3469 Dual Core CPU 最大流