【android】下载文件至本应用程序的file目录或者sdcard
一、判断是否有sdcard卡
//判断是否有SD卡 //ture:有SD卡 //false:没有SD卡 public boolean avaiableMedia(){ String status=Environment.getExternalStorageState(); if(status.equals(Environment.MEDIA_MOUNTED)){ return true; } else { return false; } }
二、下载文件至sdcard卡
if(avaiableMedia()){ HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); HttpResponse response; try { response = client.execute(get); HttpEntity entity = response.getEntity(); long length = entity.getContentLength(); InputStream is = entity.getContent(); FileOutputStream fileOutputStream = null; if (is != null) { File file = new File(Environment.getExternalStorageDirectory(),"test.apk"); fileOutputStream = new FileOutputStream(file); byte[] buf = new byte[1024]; int ch = -1; int count = 0; while ((ch = is.read(buf)) != -1) { fileOutputStream.write(buf, 0, ch); count += ch; } } fileOutputStream.flush(); if (fileOutputStream != null) { fileOutputStream.close(); } if (is != null) { is.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
三、下载文件至本应用程序的数据文件夹
else{ try { FileOutputStream outStream = this.openFileOutput("test.apk" , Context.MODE_WORLD_READABLE); HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); HttpResponse response; response = client.execute(get); HttpEntity entity = response.getEntity(); long length = entity.getContentLength(); InputStream is = entity.getContent(); int count = 0; if (is != null) { byte[] buf = new byte[1024]; int ch = -1; while ((ch = is.read(buf)) >0) { outStream.write(buf,0,ch); count += ch; Toast.makeText(getApplicationContext(),"正在下载升级包...",Toast.LENGTH_SHORT).show(); } } outStream.flush(); if (outStream != null) { outStream.close(); } if (is != null) { is.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
【android】下载文件至本应用程序的file目录或者sdcard的更多相关文章
- 【android】下载文件至本应用程序的file文件夹或者sdcard
一.推断是否有sdcard卡 //推断是否有SD卡 //ture:有SD卡 //false:没有SD卡 public boolean avaiableMedia(){ String status ...
- Android 下载文件及写入SD卡
Android 下载文件及写入SD卡,实例代码 <?xml version="1.0" encoding="utf-8"?> <LinearL ...
- android 下载文件
import com.example.android.R; import android.app.Activity;import android.os.Bundle;import android.os ...
- Android 虚拟机Dalvik、Android各种java包功能、Android相关文件类型、应用程序结构分析、ADB
Android虚拟机Dalvik Dalvik冲击 随着Google 的AndroidSDK 的发布,关于它的API 以及在移动电话领域所带来的预期影响这些方面的讨论不胜枚举.不过,其中的一个话题在J ...
- 基于Android 下载文件时,更新UI简单帮助类
因为在项目开发时.有这种简单需求,问谷歌,网络上也有好多Utils工具类,可是比較冗余.自己就简单的写了一个简单帮助类. /** * 下载文件,更新UI简单帮助类 * * @author jarlen ...
- Android下载文件到SD卡
HttpURLConnection 上传方式: 尝试理解这两种流的区别: InputStreamReader 的读取方式: //创建一个URL对象 URL url = new URL(urlStrin ...
- android 下载文件,file的读写应用
先看代码: public class MainActivity extends AppCompatActivity { String TAG = MainActivity.class.getCanon ...
- Android 下载文件 显示进度条
加入两个权限 一个是联网,另一个是读写SD卡 <uses-permission android:name="android.permission.INTERNET">& ...
- Android下载文件提示文件不存在。。。 java.io.FileNotFoundException
遇到这个错误java.io.FileNotFoundException,事实上文件是存在的,把地址复制到手机浏览器都能够直接下载的,但为嘛不能下载呢. Error in downloadBitmap ...
随机推荐
- C++白盒测试最佳实践课程,3个免费名额火热申请中,31号前截止申请...
C++白盒测试最佳实践课程,3个免费名额火热申请中,31号前截止申请...http://automationqa.com/forum.php?mod=viewthread&tid=2561&a ...
- hive--[ array、map、struct]使用
复合数据类型 Structs: structs内部的数据可以通过DOT(.)来存取,例如,表中一列c的类型为STRUCT{a INT; b INT},我们可以通过c.a来访问域a Maps(K-V对) ...
- $ git学习总结系列(3)——分支管理
本文主要介绍git分支的概念及常用分支操作. 分支的概念 所谓分支,可以理解成一个个相互独立的工作空间,在每一个分支上的改动不会影响到其他分支的代码.git默认的分支是master分支. 试想一下这样 ...
- Hadoop中RPC协议小例子报错java.lang.reflect.UndeclaredThrowableException解决方法
最近在学习传智播客吴超老师的Hadoop视频,里面他在讲解RPC通信原理的过程中给了一个RPC的小例子,但是自己编写的过程中遇到一个小错误,整理如下: log4j:WARN No appenders ...
- 使用curl 命令模拟POST/GET请求
https://blog.csdn.net/u012340794/article/details/71440604 curl命令是一个利用URL规则在命令行下工作的文件传输工具.它支持文件的上传和下载 ...
- ubuntu中在Launcher上添加Android Studio的运行图标
运行命令创建desktop文件: sudo gedit /usr/share/applications/android_studio.desktop 打开窗口后输入以下内容,注意Exec和Icon要修 ...
- 在U盘上安装Damn Small Linux
Damn Small Linux 是一个袖珍Linux发行版,整个系统只有50M左右,所以可以放到U盘中,从而可以在支持U盘启动的电脑上使用Linux,功能与LiveCD相当. 有很多种方法可以将 ...
- Python3:文件读写
Python3:文件读写 open f = open('filename','r') # 读模式 f = open('filename','w') # 写模式 f = open('filename', ...
- cordova 5.0版本说明
2015/04/21发布Cordova 5.0.0! 1)插件从Cordova plugins registry(CPR)全部移到npm,并且重新命名 ***org.apache.cordova.* ...
- linux 配置tensorflow 全过程记录
前几天刚下一个deepin系统,是基于linux 内核的,界面的设计有些mac的feel 感觉还是挺不错的,之后就赶紧配置了一下tensorflow ,尽管之前配置过,但是这次还是遇到点儿问题,所以说 ...