从android studio java中的url下载损坏的pdf

问题描述

在我的代码中,我得到了一个 pdf url,我想下载、保存并稍后显示它。 但是当从 url 下载 pdf 时,它下载了一个损坏的文件错误显示文件为空以显示它。

private class DownloadFileTask{
    public static final String TAG = "DownloadFileTask";
    private FilesPageActivity context;
    private GetTask contentTask;
    private String url;
    private String fileName;

    public DownloadFileTask(FilesPageActivity context,String url,String fileName) {
        this.context = context;
        this.url = url;
        this.fileName = fileName;
    }
    public void startTask() {
        doRequest();
    }
    private void doRequest() {
        contentTask = new GetTask();
        contentTask.execute();
    }

    private class GetTask extends AsyncTask < String,Integer,String > {
        @Override
        protected String doInBackground(String... strings) {
            int count;
            try {
                URL _url = new URL(url);
                URLConnection conection = _url.openConnection();
                conection.connect();
                String extension = url.substring(url.lastIndexOf('.') + 1).trim();
                InputStream input = new BufferedInputStream(_url.openStream(),8192);
                OutputStream output = new FileOutputStream(FilesPageActivity.this.getFilesDir() + fileName);
                byte data[] = new byte[1024];
                while ((count = input.read(data))  != -1) {
                    output.write(data,count);
                }
                output.flush();
                output.close();
                input.close();
            } catch (Exception e) {
                Log.e("Error: ",e.getMessage());
            }
            return null;
        }

        protected void onPostExecute(String data) {
            mpd.dismiss(); //ProgressDialog dismissed
        }
    }
}

谁能帮我解决这个问题??谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)