简体   繁体   English

在android中的assets/raw文件夹中的TextView中显示pdf

[英]Displaying pdf in a TextView from assets/raw folder in android

I have two activity,MainActivity.java and SampleActivity.java.I have stored a .pdf file in assets folder.我有两个活动,MainActivity.java 和 SampleActivity.java。我在资产文件夹中存储了一个 .pdf 文件。 I have a button in my first activity.When i click that button i want the pdf file to be displayed in the textview of SampleActivity.java.我的第一个活动中有一个按钮。当我单击该按钮时,我希望将 pdf 文件显示在 SampleActivity.java 的文本视图中。 The second activity also will have a download button to download the pdf file.第二个活动也将有一个下载按钮来下载 pdf 文件。

I don't want to make use of webview or display using external program.我不想使用 webview 或使用外部程序显示。 I tried with .txt files and it worked correctly but same did'nt work with pdf file.我尝试使用 .txt 文件,它工作正常,但同样不适用于 pdf 文件。

Is there any method to achieve this.有什么方法可以实现这一点。

Thanks in advance.提前致谢。

Code that i used for .txt is我用于 .txt 的代码是

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sample);

    txt=(TextView) findViewById(R.id.textView1);

    try
    {
        InputStream is=getAssets().open("hello.txt");
        int size=is.available();
        byte[] buffer=new byte[size];
        is.read(buffer);
        is.close();

        String text=new String(buffer);

        txt.setText(text);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

When i click that button i want the pdf file to be displayed in the textview of SampleActivity.java.当我单击该按钮时,我希望 pdf 文件显示在 SampleActivity.java 的文本视图中。

That is not possible.这是不可能的。 TextView cannot render a PDF. TextView无法呈现 PDF。

On Android 5.0+, there is some stuff in the printing framework to convert a PDF into images, with an eye towards print preview.在 Android 5.0+ 上,打印框架中有一些东西可以将 PDF 转换为图像,并着眼于打印预览。 You should be able to get that to work with an ImageView , though I have not played with it yet.你应该能够让它与ImageView一起工作,尽管我还没有玩过它。 Also, right now, Android 5.0+ represents ~10% of Android devices.此外,目前,Android 5.0+ 代表了约 10% 的 Android 设备。

Or, you are welcome to find a PDF rendering library that you include in your app.或者,欢迎您找到包含在您的应用程序中的 PDF 渲染库。

The second activity also will have a download button to download the pdf file.第二个活动也将有一个下载按钮来下载 pdf 文件。

That is not possible, insofar as you cannot replace a PDF file that is in assets/ .这是不可能的,因为您无法替换assets/的 PDF 文件。 Assets are read-only at runtime.资产在运行时是只读的。 You are welcome to download a PDF file to someplace that is read/write, like internal storage.欢迎您将 PDF 文件下载到可读/可写的地方,例如内部存储。 However, you still cannot display the PDF file in a TextView .但是,您仍然无法在TextView显示 PDF 文件。

I tried with .txt files and it worked correctly but same did'nt work with pdf file.我尝试使用 .txt 文件,它工作正常,但同样不适用于 pdf 文件。

In addition to all the problems mentioned above, PDF files are not text files.除了上面提到的所有问题,PDF 文件不是文本文件。 They are binary files.它们是二进制文件。 You cannot read a PDF into a String .您无法将 PDF 读入String

Below is code where you can show pdf (from Assets Folder) content in imageview in Android以下是您可以在 Android 中的 imageview 中显示 pdf(来自资产文件夹)内容的代码

private void openPDF() throws IOException {私有无效 openPDF() 抛出 IOException {

    //open file in assets


    File fileCopy = new File(getCacheDir(), "source_of_information.pdf");

    InputStream input = getAssets().open("source_of_information.pdf");
    FileOutputStream output = new FileOutputStream(fileCopy);

    byte[] buffer = new byte[1024];
    int size;
    // Copy the entire contents of the file
    while ((size = input.read(buffer)) != -1) {
        output.write(buffer, 0, size);
    }
    //Close the buffer
    input.close();
    output.close();

    // We get a page from the PDF doc by calling 'open'
    ParcelFileDescriptor fileDescriptor =
            ParcelFileDescriptor.open(fileCopy,
                    ParcelFileDescriptor.MODE_READ_ONLY);
    PdfRenderer mPdfRenderer = new PdfRenderer(fileDescriptor);
    PdfRenderer.Page mPdfPage = mPdfRenderer.openPage(0);

    // Create a new bitmap and render the page contents into it
    Bitmap bitmap = Bitmap.createBitmap(mPdfPage.getWidth(),
            mPdfPage.getHeight(),
            Bitmap.Config.ARGB_8888);
    mPdfPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);

    // Set the bitmap in the ImageView
    imageView.setImageBitmap(bitmap);
}

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

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