Android 使用Asynctask下载和保存文件工作不正常

Android 使用Asynctask下载和保存文件工作不正常,android,android-asynctask,download,Android,Android Asynctask,Download,我使用以下类下载文件并将其保存到SD卡 class DownloadFileAsync extends AsyncTask<String, String, String> { @SuppressWarnings("deprecation") @Override protected void onPreExecute() { super.onPreExecute();

我使用以下类下载文件并将其保存到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);
            }
        }

我的问题是使用上述代码时,progressBar将出现,但它将不完成而消失,并且不会进行下载。我没有注意到任何logcat错误或调试中的任何其他错误。有人能告诉我这个问题会发生什么,或者如何克服吗?

所以我的假设是正确的。在我的应用程序中,我使用静态方法返回一个文件路径,如果该路径不存在,则调用
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());“}

doInBackground
中,您没有打印
catch
块中的任何内容在
catch
块中,然后再次检查。您的try块中可能会出现异常。在catch块中放入println并检查这只是一个猜测,但是conexion.connect();应该关闭,对吗?如果您让我们知道发生了哪一个异常(从logcat),我们可能能够确定问题,可能是“PurchasedFrames“文件夹不存在。请将
Log.e()
添加到异常块,并在此处发布错误消息。请正确检查URL。
OutputStream output = new FileOutputStream(getSaveFilePath(fileName));