android - android下载大文件

标签 android eclipse

我需要从Web下载到手机的各种文件

我已经完成了代码,但是该代码只能让我以小尺寸下载图片/照片。像.apk这样的大文件大约需要2mb到> 100mb的费用,这会让我失败。
下面是代码:

final ProgressDialog progress=ProgressDialog.show(this, "Please wait", "Loading ...", true);

        new Thread()
        {
            public void run()
            {               
                try {
                    URL url = new URL("http://www.domain.com/apk/Gmail.apk");

                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                    urlConnection.setRequestMethod("GET");
                    urlConnection.setDoOutput(true);

                    urlConnection.connect();

                    File SDCardRoot = Environment.getExternalStorageDirectory();
                    File file = new File(SDCardRoot, "Gmail.apk");

                    FileOutputStream fileOutput = new FileOutputStream(file);

                    InputStream inputStream = urlConnection.getInputStream();


                    byte[] buffer = new byte[1024];
                    int bufferLength = 0; 

                    while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                            fileOutput.write(buffer, 0, bufferLength);
                            //downloadedSize += bufferLength;
                    }
                    fileOutput.close();

            } catch (MalformedURLException e) {
                    e.printStackTrace();
            } catch (IOException e) {
                    e.printStackTrace();
            }
                progress.dismiss();                     
            }
        }.start();  

最佳答案

为此,请使用以下链接的代码,它可能会对您有所帮助。

Download File

关于android - android下载大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10960411/

相关文章:

Android:执行INNER JOIN时如何从Cursor访问结果?

java - SurfaceHolder导致闪烁

android - 如何强制 EditText 以大写字母开头?

php - Eclipse Helios - 添加 PHP 可执行文件时出现问题

java - 在 SWT 标签中显示 HTML 文本

android - Google Analytics 将 Google Cloud Test Lab 测试检测为活跃用户和新用户

java - Google Maps API Android - 膨胀类 fragment 时出错

java - 如何在 Eclipse 中找到抽象方法的实现位置?

eclipse - 不允许使用命令 - 安装小程序时不满足使用条件

android - Android下如何使用Akka项目