【问题标题】:android cannot display PDF (name.pdf is of invalid format)android 无法显示 PDF(name.pdf 格式无效)
【发布时间】:2018-06-04 16:13:37
【问题描述】:

我正在使用 volley 下载文件,但无法打开它,它显示如下:

无法显示 PDF(name.pdf 格式无效)

还提到服务返回 11kb 字节 [] 和调试,我收到 16kb 响应。我使用的代码是:

InputStreamVolleyRequest request = new InputStreamVolleyRequest(Request.Method.POST, uri,
            new Response.Listener<byte[]>() {
                @Override
                public void onResponse(byte[] response) {                        
                    try {
                        if (response!=null) {

                            if(Build.VERSION.SDK_INT>22){
                                requestPermissions(new String[] {"android.permission.WRITE_EXTERNAL_STORAGE","android.permission.READ_EXTERNAL_STORAGE"}, 1);
                            }

                            File baseDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                            baseDir.mkdirs();
                            File carpeta = new File(baseDir + nombre_carpeta);
                            carpeta.mkdirs();
                            File file = new File(baseDir, documento.NombreFichero.replace("\\","/"));
                            try {
                                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
                                bos.write(response);
                                bos.flush();
                                bos.close();
                                Log.d("NEWFILE", file.getAbsolutePath());
                                Toast.makeText(getActivity(), "Descarga completa.", Toast.LENGTH_LONG).show();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }
                    } catch (Exception e) {
                        Log.d("KEY_ERROR", "UNABLE TO DOWNLOAD FILE");
                        e.printStackTrace();
                    }
                }
            } ,new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    }, (HashMap<String, String>) params);

    RequestQueue mRequestQueue = Volley.newRequestQueue(getActivity().getApplicationContext(), new HurlStack());
    mRequestQueue.add(request);

提前致谢!

【问题讨论】:

标签: android pdf android-volley


【解决方案1】:

最后我将响应作为字符串发送,然后使用 android.util.Base64 对其进行解码。这是代码:

StringRequest sr = new StringRequest(Request.Method.POST, uri, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try{
                File baseDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                baseDir.mkdirs();
                File carpeta = new File(baseDir + nombre_carpeta);
                carpeta.mkdirs();
                File file = new File(baseDir, documento.NombreFichero.replace("\\","/"));
                BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file));

                FileOutputStream fos = new FileOutputStream(file);

                byte[] respuesta = android.util.Base64.decode(response.getBytes(), android.util.Base64.DEFAULT);

                fos.write(respuesta);

                fos.flush();

                fos.close();
                output.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }

【讨论】:

    【解决方案2】:

    这不是编程问题。我建议在您的模拟器或设备中安装一个 pdf 阅读器 (Google PDF Viewer)。

    如果您使用的是模拟器,您可能需要使用已安装 Google Play 的版本。然后就可以安装pdf阅读器了。

    【讨论】:

    • 我上传到驱动器并尝试打开它并下载到我的电脑,但无法阅读 pdf
    • @Juan 你检查了你检索的内容吗?
    • @mkl 当服务发送 byte[] 时,我收到了 base64。但是解码有问题(它被解码了,但它在文件中写入了 literla Base64),所以现在我将它作为字符串 UTF_8 发送并将其字节发送到 byte[]。它适用于 .txt 文件。谢谢
    • @Juan "所以现在我将它作为字符串 UTF_8 发送并将其字节转换为 byte[]。它适用于 .txt 文件。" - 嗯,这就解释了.以 UTF-8 编码字符串发送 PDF 很可能会损坏它。 UTF-8 是一种 text 编码,因此 它适用于 .txt 文件 但 PDF 是一种 二进制格式 是有道理的,并且处理像文本这样的二进制文件总是很有可能损坏二进制数据。
    • 感谢您的回答,终于像我的回复一样解决了
    猜你喜欢
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 2021-08-29
    • 2016-05-04
    • 1970-01-01
    • 2017-10-25
    相关资源
    最近更新 更多