android实现文件下载

android实现文件下载


//1:通过I/O流下载案例:
public void downImage(final String urls,final String name) {
    //创建文件夹
    direct = new File(Environment.getExternalStorageDirectory()
            + "/MyDowns");
    if (!direct.exists()) {
        direct.mkdirs();
    }
    //开始子线程
    new Thread(new Runnable() {
        @Override
        public void run() {
            //创建文件
            file = new File(Environment.getExternalStorageDirectory()+"/MyDowns/"+name+".png");

            InputStream is = null;
            try {
                is = new URL(urls).openConnection().getInputStream();
                FileOutputStream fos = new FileOutputStream(file);
                byte[] bytes = new byte[1024];
                int len = is.read(bytes);
                while (len>0){
                    fos.write(bytes,0,len);//写入数据到file
                    len = is.read(bytes);
                }
                fos.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}
----------------------------------------------------------
//2:通过系统自带下载类 DownloadManager下载文件:
public long file_download(String uRl, String name) {
    DownloadManager dm;
    Toast.makeText(this.getApplication(), "正在下载 " + name,
            Toast.LENGTH_LONG).show();
    File direct = new File(Environment.getExternalStorageDirectory()
            + "/MyDowns");//创建文件夹
    if (!direct.exists()) {
        direct.mkdirs();
    }
    dm = (DownloadManager) this.getApplication().getApplicationContext().getSystemService
            (Context.DOWNLOAD_SERVICE);
    Uri downloadUri = Uri.parse(uRl);

    //DownloadManager.Request 用于请求一个下载
    DownloadManager.Request request = new DownloadManager.Request(
            downloadUri);

    //表示下载完成后显示通知栏提示
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setVisibleInDownloadsUi(true);

    request.setAllowedNetworkTypes(
            DownloadManager.Request.NETWORK_WIFI
                    | DownloadManager.Request.NETWORK_MOBILE)
            .setAllowedOverRoaming(false)
            .setVisibleInDownloadsUi(true)
            .setTitle(name)
            .setDescription(name)
            .setDestinationInExternalPublicDir("/MyDowns", name + ".png");
    long refernece = dm.enqueue(request);

    return refernece;
}
-------------------------------------

//3:通过xUtils第三方框架下载,需导入xUtils-2.6.14.jar架包
public void get(String urlimage,String name) {
    File dirs = new File(Environment.getExternalStorageDirectory()+"/myImage/");//创建文件夹
    if(!dirs.exists()){
        dirs.mkdirs();
    }
    File file = new File(dirs+"/"+name+".png");//创建文件路径
    HttpUtils http = new HttpUtils();
    HttpHandler handler = http.download(urlimage,
            file.toString(),
            true, // 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。
            true, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。
            new RequestCallBack<File>() {
                @Override
                public void onStart() {
                    Log.e("Start ", "Start");
                }

                @Override
                public void onLoading(long total, long current, boolean isUploading) {
                    Log.e("Loading ", "" + current + "/" + total);
                }

                @Override
                public void onSuccess(ResponseInfo<File> responseInfo) {
                    Log.e("Success ", "" + responseInfo.result.getPath());
                }

                @Override
                public void onFailure(HttpException error, String msg) {
                    Log.e("Failure ", "" + msg);
                }
            });
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值