【android】下载文件至本应用程序的file目录或者sdcard
来源:互联网 发布:access执行sql语句 编辑:程序博客网 时间:2024/05/22 14:22
一、判断是否有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();}}
1 0
- 【android】下载文件至本应用程序的file目录或者sdcard
- Android重要的文件目录mnt/sdcard 和 /sdcard
- Android SDCard文件、目录操作
- Android SDcard目录文件操作
- android 4.2版本的sdcard文件目录分析
- Android app的文件缓存目录:app的内置Flash私有目录和 外置sdcard目录
- Android SDCard文件、目录操作一
- Android 读取sdcard指定目录文件
- android文件下载及存入SDCARD
- file 读写/data/data/<应用程序名>目录上的文件
- Android应用程序相关的文件目录详解
- Android应用程序相关的文件目录详解
- android扫描SDcard的目录的图片
- Android中获取SDCard的目录路径
- android 下载文件,file的读写应用
- Android app的文件缓存目录可以是app内置私有的目录,当然也可以选择外置sdcard目录
- android 虚拟机,文件导入sdcard下报错,Read-only file system
- Android File 转Inputstram,跳转手机sdcard 获取指定文件
- NYOJ513,A+B Problem IV
- NYOJ21 【三个水杯】
- 取(2堆)石子游戏(威佐夫博弈)
- strlen和sizeof的区别
- Mesos-error
- 【android】下载文件至本应用程序的file目录或者sdcard
- android:自定义View的使用(二)
- 函数生成的ALV的按钮列表 .
- 标签个性化数据在推荐排序中的应用
- Android提高第六篇之BroadcastReceiver(广播接收者)
- Kerberos相关资料整理
- c#面试问题
- 存储过程与函数的区别
- 未能同步iPad 因为这台电脑不再被授权使用在此iPad上购买的项目