解决方案 »

  1.   

    /**
     * 把网络上的文件保存到sd卡
     * @param url 完整的可访问的url
     * @param file 文件
     * @return boolean
     */
    public boolean saveUrlFile(String url, File file) throws Exception {
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    fileSize = conn.getContentLength();
    InputStream input = conn.getInputStream();
    if (null != input) {
    copyFile(input, file);
    conn.disconnect();
    return true;
    }
    return false;
    }
    /**
     * 复制文件
     * @param input 输入流
     * @param newFile 新文件
     * @throws Exception
     */
    public void copyFile(InputStream input, File newFile) throws Exception {
    if (null != progress) {progress.setMax(fileSize);}
    OutputStream output = new FileOutputStream(newFile);
    byte[] buffer = new byte[1024];
    int i = 0, total = 0;
    while ((i = input.read(buffer)) != -1) {
    output.write(buffer, 0, i);
    total += i;
    if (null != progress) {progress.setProgress(total);}
    }
    output.flush();output.close();input.close();
    }带进度条的文件下载
      

  2.   

      progress 是什么?
      

  3.   

    进度条呀,上传下载必须要有进度条,否则很不友好的
    private int fileSize = 0;
    private ProgressDialog progress = null;gressDialog = new ProgressDialog(this);
    gressDialog.setCanceledOnTouchOutside(true);
    gressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    gressDialog.setMessage("正在下载文件...");
    gressDialog.show();
      

  4.   

    我发现你 HttpURLConnection  用完不关闭 ,还有流用完随手关闭 好习惯
      

  5.   

    亲 增加权限 在你的AndroidMainifest.xml 里在"<application "前面增加这几句话

            <!-- 在SDCard中创建与删除文件权限 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <!-- 往SDCard写入数据权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
      

  6.   

    现在问题是真机测试也下载不下来,会很慢,然后就提示“android应用无响应”。求解
      

  7.   


    真机应该是可以对SD卡操作的 我用过的
    很慢 无响应的话 貌似是不是哪里死循环了呢? 比如这里 你打印一下调试信息看看啊
    while (true) {
                    int temp = input.read(buffer, 0, buffer.length);
                    if (temp == -1) {
                        break;
                    }
                    output.write(buffer, 0, temp);
                }
      

  8.   

    你试试在异步里面下载文件吧 http://jackyrong.iteye.com/blog/1336299