5

我正在使用以下代码在 Android 中下载文件:

public class FileDownloadActivity extends Activity {
    ProgressDialog mProgressDialog;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mProgressDialog = new ProgressDialog(FileDownloadActivity.this);
        mProgressDialog.setMessage("Downlaoding File...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMax(100);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        // execute this when the downloader must be fired
        DownloadFile downloadFile = new DownloadFile();
        downloadFile.execute("http://code.google.com/android/goodies/wallpaper/android-wallpaper5_1024x768.jpg");   
    }

    private class DownloadFile extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... sUrl) {
            try {
                Log.i("","In doin bggggggggggggg");
                URL url = new URL(sUrl[0]);
                URLConnection connection = url.openConnection();
                connection.connect();

                // this will be useful so that you can show a typical 0-100% progress bar
                int fileLength = connection.getContentLength();
                // download the file
                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream("/sdcard/sample.jpg");
                byte data[] = new byte[1024];
                long total = 0;
                int count;
                while ((count = input.read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    publishProgress((int) (total * 100 / fileLength));
                    output.write(data, 0, count);
                }
                output.flush();
                output.close();
                input.close();
            } catch (Exception e) {

            }
            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog.show();
        }

        @Override  
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
            mProgressDialog.setProgress(progress[0]);
        }
    }
}

这只是向我显示过去半小时的 0% 进度。

我什至添加了

<uses-permission android:name="android.permission.INTERNET"></uses-permission>  
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>  
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>  
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>  

在清单中,甚至互联网在我的手机中都可以正常工作。

请帮忙。

谢谢斯内哈
_

4

3 回答 3

0

我的猜测是“connection.getContentLength()”在 HTTP 上不可靠并返回 0,因此您可能会得到除以零的异常。而且由于您没有记录异常,因此您在 Logcat 中看不到任何内容,也没有任何进展..

于 2012-04-11T09:30:24.147 回答
0

如果您的最低 SDK 级别为 9,请查看此类

于 2012-04-11T09:30:45.047 回答
0

lib会帮助你

https://github.com/AriaLyy/Aria

下载文件只有一个代码

Aria.download(this)
    .load(DOWNLOAD_URL)
    .setDownloadPath(DOWNLOAD_PATH) //file save path
    .add();

获取下载状态

@Download.onPre(DOWNLOAD_URL)
  protected void onPre(DownloadTask task) {}

  @Download.onTaskStart
  void taskStart(DownloadTask task) {}

  @Download.onTaskRunning
  protected void running(DownloadTask task) {}

  @Download.onTaskResume
  void taskResume(DownloadTask task) {}

  @Download.onTaskStop
  void taskStop(DownloadTask task) {}

  @Download.onTaskCancel
  void taskCancel(DownloadTask task) {}

  @Download.onTaskFail
  void taskFail(DownloadTask task) {}

  @Download.onTaskComplete
  void taskComplete(DownloadTask task) {}

  @Download.onNoSupportBreakPoint
  public void onNoSupportBreakPoint(DownloadTask task) {}
于 2017-08-08T09:07:27.620 回答