需要帮助在我的应用程序中使用java下载文件,它会尝试但很快就会消失

时间:2013-08-12 22:53:27

标签: java android

我正在尝试下载一个zip文件并将其保存到SD卡。

我有一个ID为#34的按钮;下载"

当我点击按钮时,对话框会显示并快速消失。

这是代码部分的样子。

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;

public void download(View view) {
    switch (view.getId()) {
        case R.id.download:
            startDownload();
    }
}

private void startDownload() {
    String url = "https://mydownloadurl";
    new DownloadFileAsync().execute(url);
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DIALOG_DOWNLOAD_PROGRESS:
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage("Downloading file..");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
            return mProgressDialog;

        default:
            return null;
    }
}

class DownloadFileAsync extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(DIALOG_DOWNLOAD_PROGRESS);
    }

    @Override
    protected String doInBackground(String... aurl) {
        int count;
        try {
            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();
            int lenghtOfFile = conexion.getContentLength();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/mydownload.zip");
            byte data[] = new byte[1024];
            long total = 0;
            while ((count = input.read(data)) != -1) {
                total += count;
                publishProgress("" + (int)((total * 100) / lenghtOfFile));
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
        }
        catch (Exception e) {
        }
        return null;
    }

    protected void onProgressUpdate(String... progress) {
        Log.d("ANDRO_ASYNC",progress[0]);
        mProgressDialog.setProgress(Integer.parseInt(progress[0]));
    }

    @Override
    protected void onPostExecute(String unused) {
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
    }

logcat中唯一显示的是

  

17586-17586 / com.tyler.myapp D / qdmemalloc:ion:映射缓冲区基数:0x6da9b000大小:1892352偏移量:0 fd:47   17586-17586 / com.tyler.myapp D / qdmemalloc:ion:映射缓冲区基数:0x4002a000大小:4096偏移量:0 fd:52   17586-17586 / com.tyler.myapp D / OpenGLRenderer:刷新缓存(模式0)   17586-17586 / com.tyler.myapp D / qdmemalloc:ion:取消映射缓冲区:0x6da9b000大小:1892352   17586-17586 / com.tyler.myapp D / qdmemalloc:ion:取消映射缓冲区:0x4002a000大小:4096   596-5470 / system_process W / InputMethodManagerService:窗口已经聚焦,忽略焦点增益:com.android.internal.view.IInputMethodClient$Stub$Proxy@42942fe8 attribute = null,token = android.os.BinderProxy@41dab9b0,pid = 17586 ,inputType = 0x(null)

1 个答案:

答案 0 :(得分:1)

您的代码可能在doInBackground()中引发异常。在catch块中,您应该记录异常,以便查看问题所在。你可以这样做:

Log.e("Downloader - doInBackground()", "Error while downloading", e);

另外,您是否在AndroidManifest.xml中指定了WRITE_EXTERNAL_STORAGE权限?