使用类加载器的PDF文件不开放 [英] PDF files not opening using class loader

查看:108
本文介绍了使用类加载器的PDF文件不开放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是低于code到文件链接到资源文件夹和读取文件,写,然后我正在打开通过第三方应用程序的PDF文件。

这是我的code;

 公共无效ReadFile的(){
        InputStream中的FileInputStream = NULL;
        字符串文件路径=PDF /+姓名+.PDF
        的ClassLoader类加载器= Thread.currentThread()getContextClassLoader()。
        尝试{
             的FileInputStream = classLoader.getResourceAsStream(文件路径);
             INT大小= fileInputStream.available();
             字节[]缓冲区=新的字节[大小]
             fileInputStream.read(缓冲液);
             fileInputStream.close();             WriteFile的(缓冲,名);        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }
    }    公共无效WriteFile的(字节[]缓冲区,字符串文件名){
        尝试{
            文件根= Environment.getExternalStorageDirectory();
            FileOutputStream中的FileOutputStream = NULL;            如果(root.canWrite()){
                文件pdffile =新的文件(/ SD卡/ AAA / BBB);
                pdffile.mkdirs();
                的System.out.println(PDF路径+ pdffile.toString());
                文件OUTPUTFILE =新的文件(pdffile.toString());
                的FileOutputStream =新的FileOutputStream(
                        OUTPUTFILE +/+文件名+.PDF);
                BOS的BufferedOutputStream =新的BufferedOutputStream(
                        FileOutputStream中);
                bos.write(缓冲液);
                bos.flush();
                bos.close();                }
            }赶上(IOException异常五){
            Log.e(Rrror,无法写入文件+ e.getMessage());
        }    }文件f =新的文件(/ SD卡/ AAA / BBB /+名字+PDF格式。);
URI路径= Uri.fromFile(F);
意向意图=新意图(Intent.ACTION_VIEW);
intent.setDataAndType(路径,应用程序/ PDF格式);

现在一些文件在念开放正常,但一些文件它不是打开它说氏/ P>

此文档无法打开。

但是,如果使用入资产管理器打开它完美的罚款。

文件

这里有什么问题,


解决方案

资源和大小在/ SD卡/ AAA / BBB提取的副本不同。这是由于一个错误的假设 InputStream.available()返回底层资源的正确尺寸:它只包含的,可以字节数的估计读取(或跳过)从此输入流,而不受方法的下一次调用此输入流的(根据JDK的JavaDoc)。

封锁

因此​​,你必须改变code,它复制输入流的字节数组。本课题在计算器上之前讨论过,例如比照的InputStream转换为byte []在忽略了Java (只把答案事实上这个问题指的是图像文件,如一个接@RichSeller使用Apache的commons-io的或不引入新的依赖,用一个又一个@Adamski ByteArrayOutputStream ,一个字节[] 缓冲区,循环复制,而不是结束文件( is.read(...)!= -1 ),也有可能是niftier NIO 解决方案,但到底什么...;)

I am using the below code to link files to resources folder and reading the file and writing, and then i am opening the pdf file via 3rd party app.

This is my code;

public void readFile(){
        InputStream fileInputStream= null;
        String filePath = "PDF/" + name + ".pdf";
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        try {
             fileInputStream = classLoader.getResourceAsStream(filePath);
             int size = fileInputStream.available();
             byte[] buffer = new byte[size];
             fileInputStream.read(buffer);
             fileInputStream.close();

             writeFile(buffer, name);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void writeFile( byte[] buffer, String fileName){
        try {
            File root = Environment.getExternalStorageDirectory();
            FileOutputStream fileOutputStream = null;

            if (root.canWrite()){
                File pdffile = new File("/sdcard/aaa/bbb");
                pdffile.mkdirs();
                System.out.println(" pdf path "+pdffile.toString());
                File outputFile = new File(pdffile.toString());
                fileOutputStream = new FileOutputStream(
                        outputFile+"/" + fileName + ".pdf");
                BufferedOutputStream bos = new BufferedOutputStream(
                        fileOutputStream);
                bos.write(buffer);
                bos.flush();
                bos.close();

                }
            } catch (IOException e) {
            Log.e("Rrror", "Could not write file " + e.getMessage());
        }

    }

File f = new File("/sdcard/aaa/bbb/"+name+".pdf");
Uri path = Uri.fromFile(f);
Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(path, "application/pdf");

Now some files it reading and opening properly but few files its not opening it says K

"This Document cannot be opened".

But if i use assest manager to open the files it works perfectly fine.

what is problem here,

解决方案

The sizes of the resource and the extracted copy in "/sdcard/aaa/bbb" differ. This is due to the false assumption that InputStream.available() returns the correct size of underlying resource: it only contains an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream (according to the JDK JavaDocs).

Thus you have to change the code that copies the input stream to the byte array. This subject has been discussed on StackOverflow before, e.g. cf. Convert InputStream to byte[] in Java (only regard the answers that ignore the fact that that question refers to an image file, e.g. the one by @RichSeller using Apache commons-io or, without introducing new dependencies, the one by @Adamski using a ByteArrayOutputStream, a byte[] buffer, and a loop copying while not at end-of-file (is.read(...) != -1). There might also be niftier nio solutions, but what the heck... ;)

这篇关于使用类加载器的PDF文件不开放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆