【问题标题】:Unnable to download PDF files on API 23 and lower无法下载 API 23 及更低版本的 PDF 文件
【发布时间】:2020-01-22 13:23:51
【问题描述】:

我正在尝试在我的应用程序中添加一个类,通过它我可以在 API 23 及更低版本上下载不同类型的文件而不是 pdf。

我已经在 API 24 及更高版本上测试了我的代码,我可以轻松下载 pdf 文件,但我不知道为什么它不适用于 API

public class FileDownloader {
    private static final int MEGA_BYTE = 1024 * 1024;

    public interface OnDownloadListener{
        void onStarted();
        void onProgressUpdate(int upd);
        void onFinished(String result);
        void onError(Exception e);
    }

    // usually, subclasses of AsyncTask are declared inside the activity class.
    // that way, you can easily modify the UI thread from here
    public static class DownloadTask extends AsyncTask<String, Integer, String> {

        private Context context;
        private OnDownloadListener onDownloadListener;

        public DownloadTask(Context context, OnDownloadListener onDownloadListener) {
            this.context = context;
            this.onDownloadListener = onDownloadListener;
        }

        @Override
        protected String doInBackground(String... str) {
            // take CPU lock to prevent CPU from going off if the user
            // presses the power button during download
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    getClass().getName());
            wl.acquire();

            try {
                InputStream input = null;
                OutputStream output = null;
                HttpURLConnection connection = null;
                try {
                    URL url = new URL(str[0]);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.connect();

                    // expect HTTP 200 OK, so we don't mistakenly save error report
                    // instead of the file
                    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
                        return "Server returned HTTP " + connection.getResponseCode()
                                + " " + connection.getResponseMessage();

                    // this will be useful to display download percentage
                    // might be -1: server did not report the length
                    int fileLength = connection.getContentLength();

                    // download the file
                    input = connection.getInputStream();
                    output = new FileOutputStream(str[1]);  //   /sdcard/file_name.extension

                    byte data[] = new byte[MEGA_BYTE];
                    long total = 0;
                    int count;
                    while ((count = input.read(data)) != -1) {
                        // allow canceling with back button
                        if (isCancelled())
                            return null;
                        total += count;
                        // publishing the progress....
                        if (fileLength > 0) // only if total length is known
                            publishProgress((int) (total * 100 / fileLength));
                        output.write(data, 0, count);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    if(onDownloadListener != null){
                        onDownloadListener.onError(e);
                    }
                    return e.toString();
                }finally {
                    try {
                        if (output != null)
                            output.close();
                        if (input != null)
                            input.close();
                    }catch (IOException ignored) { }

                    if (connection != null)
                        connection.disconnect();
                }
            } finally {
                wl.release();
            }
            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if(onDownloadListener != null){
                onDownloadListener.onStarted();
            }
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
            // if we get here, length is known, now set indeterminate to fals
            if(onDownloadListener != null){
                onDownloadListener.onProgressUpdate(progress[0]);
            }
        }

        @Override
        protected void onPostExecute(String result) {
            if(onDownloadListener != null){
                onDownloadListener.onFinished(result);
            }
        }
    }
}

当“HttpURLConnection”尝试连接时,它返回错误代码 404,意思是“HTTP 404 Not Found”,但在 API >= 24 上它工作正常,我也可以通过 Web 浏览器下载这些文件。 我也尝试使用“DownloadManager”类,但是当我开始在 API

如何解决 API

提前致谢。

【问题讨论】:

    标签: java android download android-download-manager


    【解决方案1】:

    将您的 minSdkVersion 设置为 15。这将使您的应用与设备 API 15 及更高版本兼容

    【讨论】:

      猜你喜欢
      • 2019-02-13
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-22
      • 2019-10-06
      相关资源
      最近更新 更多