“执行doInBackground()时发生错误”

时间:2017-01-06 10:03:39

标签: android-external-storage

我使用下面的代码来获取文件长度并使用我显示进度条直到我下载总文件但我从firebase崩溃报告中得到错误,如下所示

java.lang.RuntimeException:执行doInBackground()时发生错误 在android.os.AsyncTask $ 3.done(AsyncTask.java:304)

  @Override
  protected String doInBackground(String...Url) {
   try {
    URL url = new URL(Url[0]);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     HttpURLConnection connection = (HttpURLConnection) url.openConnection();
     connection.setRequestMethod("HEAD");
     connection.connect();
     // Detect the file lenghth
     fileLength = connection.getContentLength();
    } else {
     URLConnection connection = url.openConnection();
     connection.connect();
     // Detect the file lenghth
     fileLength = connection.getContentLength();
    }
    /*
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.setRequestMethod("HEAD");
    connection.connect();

    int fileLength = connection.getContentLength();*/

    // Locate storage location
    //  String filepath = Environment.getExternalStorageDirectory().getPath();
    String filepath = getFilesDirectory(getApplicationContext()).getPath();
    //   File fo = getFilesDirectory(getApplicationContext());
    // Download the file
    InputStream input = new BufferedInputStream(url.openStream());

    // Save the downloaded file
    if (lang_slector == 0) {
     // input stream to read file - with 8k buffer
     File folder = new File(filepath, "/offlinedata/");
     folder.mkdir();
     File pdfFile = new File(folder, englishpdffilename);
     try {
      pdfFile.createNewFile();
     } catch (IOException e) {
      e.printStackTrace();
     }
     OutputStream output = new FileOutputStream(filepath + "/offlinedata/" +
      pdffilename);
     byte data[] = new byte[1024];
     long total = 0;
     int count;
     while ((count = input.read(data)) != -1) {
      total += count;
      // Publish the progress
      publishProgress((int)(total * 100 / fileLength));

      output.write(data, 0, count);
     }
     // Close connection
     output.flush();
     output.close();
     input.close();
    } else if (lang_slector == 1) {
     // input stream to read file - with 8k buffer
     File folder = new File(filepath, "/offlinedata/");
     folder.mkdir();
     File pdfFile = new File(folder, englishpdffilename);
     try {
      pdfFile.createNewFile();
     } catch (IOException e) {
      e.printStackTrace();
     }
     OutputStream output = new FileOutputStream(filepath + "/offlinedata/" +
      englishpdffilename);
     byte data[] = new byte[1024];
     long total = 0;
     int count;
     while ((count = input.read(data)) != -1) {
      total += count;
      // Publish the progress
      publishProgress((int)(total * 100 / fileLength));

      output.write(data, 0, count);
     }

     // Close connection
     output.flush();
     output.close();
     input.close();
    }

   } catch (Exception e) {
    // Error Log
    Log.e("Error", e.getMessage());
    e.printStackTrace();
   }
   return null;
  }

0 个答案:

没有答案