一、判断是否有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. boost智能指针总结

    智能指针是一种具备指针类似行为的对象,当不在需要它的时候自动删除其引用的c++对象.直接点说就是自动析构C++对象. boost提供了6种智能指针,如下所示: scoped_ptr <boost ...

  2. 子元素绝对定位absolute后,自动撑开宽度

    position: absolute;   white-space: nowrap;

  3. akka框架地址

    http://doc.akka.io/docs/akka/2.2.3/AkkaJava.pdf

  4. iOS 自定义滑动切换TabbarItem 觉得设计丑也要做出来的UI效果。。。

    UI丑却要继续做的感言: 对UI不满意的时候,就会觉得丑爆了,时间长了,却丑习惯了. 论前一阵子Tabbar 多丑,丑得最后不要tabbar了...但是自定义tabbar 和遇到的问题解决的过程可以记 ...

  5. iOS 在cell中使用倒计时的处理方法(新)

    一.前言 之前的文章iOS 在cell中使用倒计时的处理方法得到大量的支持, 在这先感谢大家的支持. 但是也收到不少人的回复表示不会用, 需要一一解答, 由于之前写的时候没有使用Markdown编辑, ...

  6. Web前端学习笔记之BootStrap

    Bootstrap介绍 Bootstrap是Twitter开源的基于HTML.CSS.JavaScript的前端框架. 它是为实现快速开发Web应用程序而设计的一套前端工具包. 它支持响应式布局,并且 ...

  7. Introspector内省和反射的区别.

    Introspector 是一个专门处理bean的工具类.用来获取Bean体系里的 propertiesDescriptor,methodDescriptor. 要理解这个,就要理解下面几个议题.   ...

  8. Spring Boot 中全局异常处理器

    Spring Boot 中全局异常处理器,就是把错误异常统一处理的方法.等价于Springmvc中的异常处理器. 步骤一:基于前面的springBoot入门小demo修改 步骤二:修改HelloCon ...

  9. LeetCode——Hamming Distance

    LeetCode--Hamming Distance Question The Hamming distance between two integers is the number of posit ...

  10. windchill系统——开发_菜单栏添加选项(模型添加action)

    目录:C:\ptc\Windchill_11.0\Windchill\codebase\config\actions 文件:custom-actionModels.xml和custom-actions ...