【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 ...
随机推荐
- HZNUOJ 2508:双峰插云
我们注意到总的颜色种类只有200,故我们可以先处理出所有云朵中每一种颜色有多少朵云朵拥有,此处我们约定用$cnt[] 表示$ 然后暴力枚举将哪朵云分给老薛,记录三个临时变量$Max_{kk}, Max ...
- HDOJ 1501 Zipper 【DP】【DFS+剪枝】
HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- hadoop2.7.x运行wordcount程序卡住在INFO mapreduce.Job: Running job:job _1469603958907_0002
一.抛出问题 Hadoop集群(全分布式)配置好后,运行wordcount程序测试,发现每次运行都会卡住在Running job处,然后程序就呈现出卡死的状态. wordcount运行命令:[hado ...
- Mysql5.7 用户与授权
mysql -uroot -proot MySQL5.7 mysql.user表没有password字段改 authentication_string: 一. 创建用户: 命令:CREATE USER ...
- 剑指offer编程题66道题 36-66
36.两个链表的第一个公共节点 题目描述 输入两个链表,找出它们的第一个公共结点. 1.具有重合节点的两个链表是一个Y字性,用两个堆栈放这两个链表,从尾部开始遍历,直到遍历到最后一个重合节点. 这种算 ...
- DbEntry.Net(Lephone Framework) Access ORM:安装和简单使用
项目中用到Access数据库,之前用的普通Ado.Net 三层.遇到表字段叫多时,就比较费力.想要使用ORM,无奈EF不支持Access.虽然可以改写linq to sql为Linq to Acces ...
- DateTable To Json
private string aaaa(DataTable dt) { JavaScriptSerializer javaScriptSerializer = new JavaScriptSerial ...
- 把 html标签转化为 html标签代码
HttpUtility.HtmlEncode(table.Rows[0]["footnote"].ToString());
- Python3:数字类型和字符串类型的相互转换
Python3:数字类型和字符串类型的相互转换 一.python中字符串转换成数字 方法1: 类中进行导入:import string str='555'num=string.atoi(str)num ...
- 将CString写入到本地文件中
void SocketU::WritePacket2File(CString packet_str)//packet_str为待写入的字符串{ CTime time = CTime::GetCurre ...