简体   繁体   English

PDF渲染器类未在Android SDK 21上渲染PDF

[英]The PDF renderer class is not rendering a PDF on Android SDK 21

Here's the code I'm using to render a PDF called "answerkey.pdf" that's stored in "Download/Adobe Reader/answerkey.pdf" 这是我用来呈现称为“ answerkey.pdf”的PDF的代码,该PDF存储在“下载/ Adob​​e Reader / answerkey.pdf”中

package com.practice.pdftest;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.pdf.PdfRenderer;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MainActivity extends Activity {


    private ImageView imageView;
    private Button next, previous;
    private TextView tv;
    private int currentPage = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        next = (Button)findViewById(R.id.next);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentPage++;
                render();
            }
        });

        previous = (Button)findViewById(R.id.previous);
        previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentPage--;
                render();
            }
        });
//        tv = (TextView)findViewById(R.id.testText);
//        tv.setText(Environment.getExternalStorageDirectory().getAbsolutePath());
//
//        Toast toast = Toast.makeText(getApplicationContext(),Environment.getExternalStorageDirectory().getAbsolutePath(),Toast.LENGTH_LONG);
//        toast.show();
    }

    private void render() {
        try {
            imageView = (ImageView)findViewById(R.id.imageView);

            int REQ_WIDTH = 1;
            int REQ_HEIGHT = 1;

            REQ_WIDTH = imageView.getWidth();
            REQ_HEIGHT = imageView.getHeight();
            Bitmap bitmap = Bitmap.createBitmap(REQ_WIDTH, REQ_HEIGHT, Bitmap.Config.ARGB_4444);
            File file = new File(Environment.getDataDirectory().getPath()+"Adobe Reader/answerkey.pdf");
            PdfRenderer pdfRenderer = new PdfRenderer(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY));
            if(currentPage < 0) {
                currentPage=0;
            }
            else if (currentPage > pdfRenderer.getPageCount()) {
                currentPage = pdfRenderer.getPageCount()-1;
            }

            Matrix matrix = imageView.getImageMatrix();
            Rect rect = new Rect(0,0,REQ_HEIGHT,REQ_WIDTH);
            pdfRenderer.openPage(currentPage).render(bitmap,rect,matrix,PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
            imageView.setImageMatrix(matrix);
            imageView.setImageBitmap(bitmap);
            imageView.invalidate();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Here's the layout.xml file I've made - 这是我制作的layout.xml文件-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.practice.pdftest.MainActivity">

   <ImageView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/imageView"
       android:layout_weight="4"
       android:background="@android:color/white"
       android:layout_marginBottom="20dp"
       />
    <!--<TextView-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:id="@+id/testText"-->
        <!--android:text="Test"-->

        <!--/>-->
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_weight="2"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:layout_marginTop="-100dp">

        <Button
            android:id="@+id/previous"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="Previous"
            />

        <Button
            android:id="@+id/next"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="Next" />

    </LinearLayout>
</LinearLayout>

For some reason, the PDF just isn't being displayed on screen, I keep getting a white, blank background. 由于某种原因,PDF并未在屏幕上显示,我一直得到白色空白背景。 What am I doing wrong? 我究竟做错了什么? Is the path incorrect? 路径不正确吗? I have no SD card on my device. 我的设备上没有SD卡。 Or am I doing something wrong with the bitmap? 还是我对位图做错了?

Is the path incorrect? 路径不正确吗?

Yes. 是。

I have no SD card on my device 我的设备上没有SD卡

That is fine. 那也行。 That would be removable storage . 那将是可移动存储 I am interpreting "Download/Adobe Reader/answerkey.pdf" as referring to something on external storage . 我将“ Download / Adob​​e Reader / answerkey.pdf”解释为是指外部存储上的某些内容。

The recommended way to get this location would be to replace: 推荐的获取该位置的方法是替换:

File file = new File(Environment.getDataDirectory().getPath()+"Adobe Reader/answerkey.pdf");

with: 与:

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Adobe Reader/answerkey.pdf");

Note that your app will need to request the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permission, and that will involve runtime permissions on Android 6.0+ (if your targetSdkVersion is 23 or higher). 请注意,您的应用将需要请求READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE权限,而这将涉及Android 6.0+上的运行时权限 (如果targetSdkVersion为23或更高)。

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

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