3

以下は、データベースAsyncTaskからPDFファイルをダウンロードすることです。CloudBoostコードは機能し、ファイルをダウンロードします。ただし、返されるファイルの長さが-1. 誰かがこれに対処する方法についてのヒントを教えてもらえますか.

ちなみにloading.setProgress(progress[0]);progress updateメソッドの行は、このAsyncTaskクラスがネストされているクラスの先頭で初期化されています。

class DownloadPdfFromInternet extends AsyncTask<String, Integer, String> {

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

    @Override
    protected String doInBackground(String... strings) {
        int count;
        try {
            URL url = new URL(strings[0]);
            URLConnection connection = url.openConnection();
            connection.connect();
            // get length of file
            int lengthOfFile = connection.getContentLength();
            Log.d("dozer74", "LengthOf File: " + lengthOfFile);

            InputStream input = new BufferedInputStream(url.openStream(), 10 * 1024);
            OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/" + pubName);
            byte data[] = new byte[1024];
            long total = 0;
            while ((count = input.read(data)) != -1) {
                total += count;
                publishProgress((int) (total * 100 / lengthOfFile));
                // write data to file
                output.write(data, 0, count);
            }
            output.flush();
            output.close();
            input.close();
        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }
        return null;
    }

    protected void onProgressUpdate(Integer... progress) {
        loading.setProgress(progress[0]);
    }

    @Override
    protected void onPostExecute(String url) {
        loading.dismiss();
        openPdfFile();
    }

}
4

0 に答える 0