pushandpop
pushandpop

Reputation: 505

How to render PDF on Android using PdfRenderer

I'm failing on this task in both cases:

1. Trying to open it from the phone memory
2. Trying to open it from the assets

I have render() method which is in 1st case looking like this:

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

        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.getExternalStorageDirectory() + "/Download/test.pdf");

        PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY));

        if (currentPage < 0) {
            currentPage = 0;
        } else if (currentPage > renderer.getPageCount()) {
            currentPage = renderer.getPageCount() - 1;
        }

        Matrix matrix = imageView.getImageMatrix();
        Rect rect = new Rect(0, 0, REQ_WIDTH, REQ_HEIGHT);

        renderer.openPage(currentPage).render(bitmap, rect, matrix, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);

        imageView.setImageMatrix(matrix);
        imageView.setImageBitmap(bitmap);
        imageView.invalidate();
        renderer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
}

Most interesting part of thrown exception is here. In case you want to see whole logs click here.

And yes, I've added needed permission in AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.john.pdfreader">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".BookActivity">
            <intent-filter>
                    <action android:name="com.example.schoolreader.BookActivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Let's take a look on the 2nd case(reading from the assets):

// ...previous code
ParcelFileDescriptor input = getAssets().openFd("test.pdf").getParcelFileDescriptor();
PdfRenderer renderer = new PdfRenderer(input);
// ..previous code

In this case it says the same, but if I remember correctly there was smth saying cannot create document, but now it's gone and exception looks the same. Anyway, why did I start talking about 2nd case? There's an open issue saying that META-INF should be at the end of the apk. Mine is the at the end and it still not working.

Frankly, I've tried even an example from Androidstudio itself. And you know what? It's working! But only with their sample.pdf and when I try to replace it with mine it just crashes. Do you have any idea what I can try to make pdf rendering on my Android?

Everything was tested on Nexus 5, Android 6.0 Marshmallow.

Upvotes: 2

Views: 8705

Answers (1)

Marc
Marc

Reputation: 106

In addition to the permission in the manifest, you may need to ask for permission explicitly. Something like:

int permission = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);

if (permission != PackageManager.PERMISSION_GRANTED)
{
    // We don't have permission so prompt the user
    ActivityCompat.requestPermissions(getActivity(), PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
}

Upvotes: 1

Related Questions