一、判断是否有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的更多相关文章

  1. 【android】下载文件至本应用程序的file文件夹或者sdcard

     一.推断是否有sdcard卡 //推断是否有SD卡 //ture:有SD卡 //false:没有SD卡 public boolean avaiableMedia(){ String status ...

  2. Android 下载文件及写入SD卡

    Android 下载文件及写入SD卡,实例代码 <?xml version="1.0" encoding="utf-8"?> <LinearL ...

  3. android 下载文件

    import com.example.android.R; import android.app.Activity;import android.os.Bundle;import android.os ...

  4. Android 虚拟机Dalvik、Android各种java包功能、Android相关文件类型、应用程序结构分析、ADB

    Android虚拟机Dalvik Dalvik冲击 随着Google 的AndroidSDK 的发布,关于它的API 以及在移动电话领域所带来的预期影响这些方面的讨论不胜枚举.不过,其中的一个话题在J ...

  5. 基于Android 下载文件时,更新UI简单帮助类

    因为在项目开发时.有这种简单需求,问谷歌,网络上也有好多Utils工具类,可是比較冗余.自己就简单的写了一个简单帮助类. /** * 下载文件,更新UI简单帮助类 * * @author jarlen ...

  6. Android下载文件到SD卡

    HttpURLConnection 上传方式: 尝试理解这两种流的区别: InputStreamReader 的读取方式: //创建一个URL对象 URL url = new URL(urlStrin ...

  7. android 下载文件,file的读写应用

    先看代码: public class MainActivity extends AppCompatActivity { String TAG = MainActivity.class.getCanon ...

  8. Android 下载文件 显示进度条

    加入两个权限 一个是联网,另一个是读写SD卡 <uses-permission android:name="android.permission.INTERNET">& ...

  9. Android下载文件提示文件不存在。。。 java.io.FileNotFoundException

    遇到这个错误java.io.FileNotFoundException,事实上文件是存在的,把地址复制到手机浏览器都能够直接下载的,但为嘛不能下载呢. Error in downloadBitmap ...

随机推荐

  1. nginx灰度环境

    1.nginx.conf split_clients "${remote_addr}AAA" $request_type { 25% "abtest"; * & ...

  2. hadoop13---centos安装jdk

    由于各Linux开发厂商的不同,因此不同开发厂商的Linux版本操作细节也不一样,今天就来说一下CentOS下JDK的安装: 方法一:手动解压JDK的压缩包,然后设置环境变量 1.在/usr/目录下创 ...

  3. Block作为返回值时的使用

    昨天使用一个小例子简单说明了下Block作为参数时的使用. 今天再来复习一下Block作为返回值使用时的情况,先贴一小段热门第三方框架Masonry的官方sample code: [view1 mas ...

  4. Linux系统下使用pwgen生成密码的使用教程

    pwgen生成的密码易于记忆且相当安全.从技术上来说,容易记忆的密码不会比随机生成的密码更加安全.但是,在大多数情况下,pwgen生成的密码已经足够安全,除了网银密码等需要高安全等级的情况外.使用易于 ...

  5. Linux 一键安装最新内核并开启 BBR 脚本

    原文链接   https://teddysun.com/489.html 请到原文链接仔细阅读后操作.建议查看过脚本内容后操作,方便理解运行过程. 使用root用户登录,运行以下命令: wget -- ...

  6. Flume-NG源码阅读之SinkGroups和SinkRunner

    在AbstractConfigurationProvider类中loadSinks方法会调用loadSinkGroups方法将所有的sink和sinkgroup放到了Map<String, Si ...

  7. BZOJ4455/UOJ185 [Zjoi2016]小星星

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. CodeChef CHEFSOC2 Chef and Big Soccer 水dp

    Chef and Big Soccer   Problem code: CHEFSOC2 Tweet     ALL SUBMISSIONS All submissions for this prob ...

  9. iOS CoreData版本升级和数据库迁移

    app中使用了CoreData,并且在下一个版本中有实体变动,比如实体新增字段.修改字段等改动, 那么app在覆盖安装时就要进行数据库迁移, 否则app就会crash. 那如何实现数据库迁移呢?大概需 ...

  10. ubuntu 14.04中安装 ruby on rails 环境(填坑版) 呕血推荐

    环境:在win7 上Vmware虚拟机环境中安装的ubuntu 14.04 开发相关: ruby 2.2.0 rails 4.2.0 sublime text 3 本文说明:所有的命令均在$ 之后,若 ...