自己封装了个下载工具类,包括有进度条和无进度条~

自己封装了个下载工具类,包括有进度条和无进度条~

public class DownLoadUtils {

    //无进度条下载
    public static void downLoadFile(Context context, String httpUrl, final String fileName) {
        int fileSize;
        // TODO Auto-generated method stub
        File tmpFile = new File("/sdcard/update");
        if (!tmpFile.exists()) {
            tmpFile.mkdir();
        }
        final File file = new File("/sdcard/update/" + fileName);

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection conn = (HttpURLConnection) url
                    .openConnection();
            InputStream is = conn.getInputStream();
            FileOutputStream fos = new FileOutputStream(file);

            byte[] buf = new byte[1024];
            conn.connect();
            double count = 0;
            fileSize = conn.getContentLength();//文件大小

            //判断内存是否够用
            long sdFreeSize = SDCardUtils.getSDFreeSize() * 1024 * 1024;
            Log.i("ruin", "剩余内存大小-- " + sdFreeSize);
            if (sdFreeSize < fileSize) {
                Toast.makeText(context, "您的内存不足", Toast.LENGTH_SHORT).show();
                return;
            }

            if (conn.getResponseCode() >= 400) {
                Toast.makeText(context, "连接超时", Toast.LENGTH_SHORT).show();
            } else {

                while (count <= 100) {
                    if (is != null) {
                        int numRead = is.read(buf);

                        if (numRead <= 0) {
                            break;
                        } else {
                            fos.write(buf, 0, numRead);
                        }
                    } else {
                        break;
                    }
                }
                Log.i("ruin", "文件下载成功");
            }
            conn.disconnect();
            fos.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //有进度条下载
    public static void downLoadFile(Context context, String httpUrl, final String fileName, ShowProgress showProgress) {
        int progress = 0;
        int downLoadFileSize = 0;
        showProgress.show(progress);
        int fileSize;
        // TODO Auto-generated method stub
        File tmpFile = new File("/sdcard/Pictures");
        if (!tmpFile.exists()) {
            tmpFile.mkdir();
        }
        final File file = new File("/sdcard/Pictures/" + fileName);

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection conn = (HttpURLConnection) url
                    .openConnection();
            InputStream is = conn.getInputStream();
            FileOutputStream fos = new FileOutputStream(file);

            byte[] buf = new byte[1024];
            conn.connect();
            double count = 0;
            fileSize = conn.getContentLength();//文件大小

            //判断内存是否够用
            long sdFreeSize = SDCardUtils.getSDFreeSize() * 1024 * 1024;
            Log.i("ruin", "剩余内存大小-- " + sdFreeSize);
            if (sdFreeSize < fileSize) {
                Toast.makeText(context, "您的内存不足", Toast.LENGTH_SHORT).show();
                return;
            }

            if (conn.getResponseCode() >= 400) {
                Toast.makeText(context, "连接超时", Toast.LENGTH_SHORT).show();
            } else {
                while (count <= 100) {
                    if (is != null) {
                        int numRead = is.read(buf);
                        if (numRead <= 0) {
                            break;
                        } else {
                            downLoadFileSize += numRead;
                            progress = downLoadFileSize * 100 / fileSize;
                            showProgress.change(progress);
                            fos.write(buf, 0, numRead);
                        }
                    } else {
                        break;
                    }
                }
                showProgress.dismiss();
                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
                Log.i("ruin", "文件下载成功");
            }
            conn.disconnect();
            fos.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface ShowProgress {
        void show(int progress);//显示进度条
        void change(int progress);//更新进度条
        void dismiss();//隐藏进度条
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值