简体   繁体   English

如何使用位图从res文件夹打开PDF文件?

[英]How to open PDF file from res folder using bitmap?

I am trying to read a PDF file name main.pdf from raw folder and then open it on an ImageView. 我试图从原始文件夹中读取PDF文件名main.pdf,然后在ImageView上打开它。 It's showing no error, but the bitmap is returning null. 它没有显示错误,但位图返回null。 The main purpose of my application is to open only one PDF file which will be embedded in the application, and users can read only this PDF file. 我的应用程序的主要目的是只打开一个嵌入应用程序的PDF文件,用户只能读取这个PDF文件。 If there is another solution, you can suggest me. 如果有其他解决方案,你可以建议我。

private void read()
{
    InputStream in;
    try
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        in = getResources().openRawResource(R.raw.main);
        System.out.println("input stream = " + in);

        BufferedInputStream buffer = new BufferedInputStream(in);
        System.out.println("buffer stream = " + buffer);
        Bitmap bitmap = BitmapFactory.decodeStream(buffer, null, options);
        System.out.println("bitmap = " + bitmap);

        Matrix imageMatrix = image.getImageMatrix();
        image.setImageMatrix(imageMatrix);
        image.setImageBitmap(bitmap);
        image.invalidate();
        in.close();
    } catch (Exception e)
    {
        Log.e("tag", e.getMessage());
        System.err.print(e);
    }
}

Like what @greenapps has said, BitmapFactory cannot open PDF files. 就像@greenapps所说的那样,BitmapFactory无法打开PDF文件。

What exactly are you trying to accomplish? 你到底想要完成什么? Is your app primarily a PDF reader app, or is PDF reading a minor feature? 您的应用主要是PDF阅读器应用,还是PDF阅读小功能?

If it's the latter, I suggest that you look into 3rd party libraries such as AndroidPdfViewer or AndroidPdfViewerV1 to view PDF files in your app. 如果是后者,我建议您查看第三方库,如AndroidPdfViewerAndroidPdfViewerV1,以查看应用中的PDF文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM