使用 Asynctask 下载和保存文件无法正常工作



我使用以下类下载文件并将其保存到SD卡。

class DownloadFileAsync extends AsyncTask<String, String, String> {
            @SuppressWarnings("deprecation")
            @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(Environment.getExternalStorageDirectory().getAbsolutePath()+"PurchasedFrames/"+filename);
            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) {
                File file = new File("/sdcard/PurchasedFrames/", filename );
                Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                     ImageView frame = (ImageView) findViewById(R.id.frame);
                     frame.setImageBitmap(myBitmap);
                dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            }
        }

对话框方法

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;
        }
    }

我使用上述代码的问题,进度条会出现,但它会在没有完成的情况下消失,也不会发生下载。我无法注意到任何日志猫错误或调试中的任何其他错误。谁能说出会发生什么或如何克服这个问题?

所以我的

假设是正确的。在我的应用程序中,我使用了一个静态方法来返回一个文件路径,如果该路径不存在,则调用mkdirs()

对于您的代码,此方法如下所示:

public static File getSaveFilePath(String fileName) {
    File dir = new File(Environment.getExternalStorageDirectory(), "PurchasedFrames");
    dir.mkdirs();
    File file = new File(dir, fileName);
    return file;
}

然后将该行替换为输出流:

OutputStream output = new FileOutputStream(getSaveFilePath(fileName));

同时将异常块替换为catch (Exception e) { Log.e("DownloadFileAsync", e.toString()); "}

最新更新