Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio - PDFRendererBasic - Unable to replace sample.pdf?

Everytime I edit the PDFRendererBasicFragment java code

mFileDescriptor = context.getAssets().openFd("sample.pdf").getParcelFileDescriptor();

to a different pdf placed in the assets folder, the application crashes..

Logcat reports:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.pdfrendererbasic/com.example.android.pdfrendererbasic.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.pdf.PdfRenderer.getPageCount()' on a null object reference

Why is this happening? I even tried renaming my PDF to sample.pdf before building and it still crashes.. Everything works fine with the original code, just wont let me replace the PDF.

Stack Trace:

02-11 11:11:50.130 18201-18201/com.example.android.pdfrendererbasic I/art: Not late-enabling -Xcheck:jni (already on)
02-11 11:11:50.130 18201-18201/com.example.android.pdfrendererbasic I/art: Late-enabling JIT
02-11 11:11:50.132 18201-18201/com.example.android.pdfrendererbasic I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
02-11 11:11:50.201 18201-18201/com.example.android.pdfrendererbasic W/System: ClassLoader referenced unknown path: /data/app/com.example.android.pdfrendererbasic-1/lib/x86
02-11 11:11:50.252 18201-18201/com.example.android.pdfrendererbasic W/System.err: java.io.IOException: cannot create document. Error: 3
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.graphics.pdf.PdfRenderer.nativeCreate(Native Method)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.graphics.pdf.PdfRenderer.<init>(PdfRenderer.java:153)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.openRenderer(PdfRendererBasicFragment.java:141)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.onAttach(PdfRendererBasicFragment.java:108)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.Fragment.onAttach(Fragment.java:1380)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:932)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.BackStackRecord.run(BackStackRecord.java:793)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.FragmentController.execPendingActions(FragmentController.java:325)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.Activity.performStart(Activity.java:6252)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.os.Looper.loop(Looper.java:148)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
02-11 11:11:50.253 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
02-11 11:11:50.254 18201-18201/com.example.android.pdfrendererbasic W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-11 11:11:51.082 18201-18201/com.example.android.pdfrendererbasic D/AndroidRuntime: Shutting down VM
02-11 11:11:51.082 18201-18201/com.example.android.pdfrendererbasic E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: com.example.android.pdfrendererbasic, PID: 18201
                                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.pdfrendererbasic/com.example.android.pdfrendererbasic.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.pdf.PdfRenderer.getPageCount()' on a null object reference
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                          at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                          at android.os.Looper.loop(Looper.java:148)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.pdf.PdfRenderer.getPageCount()' on a null object reference
                                                                                          at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.showPage(PdfRendererBasicFragment.java:163)
                                                                                          at com.example.android.pdfrendererbasic.PdfRendererBasicFragment.onViewCreated(PdfRendererBasicFragment.java:101)
                                                                                          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:988)
                                                                                          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
                                                                                          at android.app.BackStackRecord.run(BackStackRecord.java:793)
                                                                                          at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
                                                                                          at android.app.FragmentController.execPendingActions(FragmentController.java:325)
                                                                                          at android.app.Activity.performStart(Activity.java:6252)
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                          at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                          at android.os.Looper.loop(Looper.java:148) 
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

PDFRendererBasicFragment.java:

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.pdfrendererbasic;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.pdf.PdfRenderer;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.IOException;

/**
 * This fragment has a big {@ImageView} that shows PDF pages, and 2 {@link android.widget.Button}s to move between
 * pages. We use a {@link android.graphics.pdf.PdfRenderer} to render PDF pages as {@link android.graphics.Bitmap}s.
 */
public class PdfRendererBasicFragment extends Fragment implements View.OnClickListener {

    /**
     * Key string for saving the state of current page index.
     */
    private static final String STATE_CURRENT_PAGE_INDEX = "current_page_index";

    /**
     * File descriptor of the PDF.
     */
    private ParcelFileDescriptor mFileDescriptor;

    /**
     * {@link android.graphics.pdf.PdfRenderer} to render the PDF.
     */
    private PdfRenderer mPdfRenderer;

    /**
     * Page that is currently shown on the screen.
     */
    private PdfRenderer.Page mCurrentPage;

    /**
     * {@link android.widget.ImageView} that shows a PDF page as a {@link android.graphics.Bitmap}
     */
    private ImageView mImageView;

    /**
     * {@link android.widget.Button} to move to the previous page.
     */
    private Button mButtonPrevious;

    /**
     * {@link android.widget.Button} to move to the next page.
     */
    private Button mButtonNext;

    public PdfRendererBasicFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_pdf_renderer_basic, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // Retain view references.
        mImageView = (ImageView) view.findViewById(R.id.image);
        mButtonPrevious = (Button) view.findViewById(R.id.previous);
        mButtonNext = (Button) view.findViewById(R.id.next);
        // Bind events.
        mButtonPrevious.setOnClickListener(this);
        mButtonNext.setOnClickListener(this);
        // Show the first page by default.
        int index = 0;
        // If there is a savedInstanceState (screen orientations, etc.), we restore the page index.
        if (null != savedInstanceState) {
            index = savedInstanceState.getInt(STATE_CURRENT_PAGE_INDEX, 0);
        }
        showPage(index);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            openRenderer(activity);
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(activity, "Error! " + e.getMessage(), Toast.LENGTH_SHORT).show();
            activity.finish();
        }
    }

    @Override
    public void onDetach() {
        try {
            closeRenderer();
        } catch (IOException e) {
            e.printStackTrace();
        }
        super.onDetach();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (null != mCurrentPage) {
            outState.putInt(STATE_CURRENT_PAGE_INDEX, mCurrentPage.getIndex());
        }
    }

    /**
     * Sets up a {@link android.graphics.pdf.PdfRenderer} and related resources.
     */
    private void openRenderer(Context context) throws IOException {
        // In this sample, we read a PDF from the assets directory.
        mFileDescriptor = context.getAssets().openFd("sample.pdf").getParcelFileDescriptor();
        // This is the PdfRenderer we use to render the PDF.
        mPdfRenderer = new PdfRenderer(mFileDescriptor);
    }

    /**
     * Closes the {@link android.graphics.pdf.PdfRenderer} and related resources.
     *
     * @throws java.io.IOException When the PDF file cannot be closed.
     */
    private void closeRenderer() throws IOException {
        if (null != mCurrentPage) {
            mCurrentPage.close();
        }
        mPdfRenderer.close();
        mFileDescriptor.close();
    }

    /**
     * Shows the specified page of PDF to the screen.
     *
     * @param index The page index.
     */
    private void showPage(int index) {
        if (mPdfRenderer.getPageCount() <= index) {
            return;
        }
        // Make sure to close the current page before opening another one.
        if (null != mCurrentPage) {
            mCurrentPage.close();
        }
        // Use `openPage` to open a specific page in PDF.
        mCurrentPage = mPdfRenderer.openPage(index);
        // Important: the destination bitmap must be ARGB (not RGB).
        Bitmap bitmap = Bitmap.createBitmap(mCurrentPage.getWidth(), mCurrentPage.getHeight(),
                Bitmap.Config.ARGB_8888);
        // Here, we render the page onto the Bitmap.
        // To render a portion of the page, use the second and third parameter. Pass nulls to get
        // the default result.
        // Pass either RENDER_MODE_FOR_DISPLAY or RENDER_MODE_FOR_PRINT for the last parameter.
        mCurrentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
        // We are ready to show the Bitmap to user.
        mImageView.setImageBitmap(bitmap);
        updateUi();
    }

    /**
     * Updates the state of 2 control buttons in response to the current page index.
     */
    private void updateUi() {
        int index = mCurrentPage.getIndex();
        int pageCount = mPdfRenderer.getPageCount();
        mButtonPrevious.setEnabled(0 != index);
        mButtonNext.setEnabled(index + 1 < pageCount);
        getActivity().setTitle(getString(R.string.app_name_with_index, index + 1, pageCount));
    }

    /**
     * Gets the number of pages in the PDF. This method is marked as public for testing.
     *
     * @return The number of pages.
     */
    public int getPageCount() {
        return mPdfRenderer.getPageCount();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.previous: {
                // Move to the previous page
                showPage(mCurrentPage.getIndex() - 1);
                break;
            }
            case R.id.next: {
                // Move to the next page
                showPage(mCurrentPage.getIndex() + 1);
                break;
            }
        }
    }

}
like image 469
emvee Avatar asked Feb 11 '16 17:02

emvee


2 Answers

Downgrade gradle version from 2.2.0 to 2.1.2 and crash will go away.

In the build.gradle(Project) replace

classpath 'com.android.tools.build:gradle:2.2.0'

with

classpath 'com.android.tools.build:gradle:2.1.2'
like image 57
Naveed Alam Tareen Avatar answered Nov 03 '22 00:11

Naveed Alam Tareen


Same issue as you mentioned is reported and described here. Problem seems to be with the way Android Studio is creating the final apk file. While solution to manually repack the apk file is provided in the given link it is not very practical. The error seems not to have any other simple solution.

EDIT

Here is the solution from the provided link. The reported issue however mentions slightly different error message, so this may not work.

  1. Start Android Studio
  2. Click Import an Android code sample
  3. Select Graphics > Pdf Renderer Basic
  4. Click Next
  5. Click Finish.
  6. Click Run > Run 'Application'

Running the application implicitly creates the Application/build/outputs/apk/Application-debug.apk file.

6.a: Sanity check: Verify that the META-INF entries are at the end of the file:

$ unzip -lv Application/build/outputs/apk/Application-debug.apk
...
    1569  Defl:N      702  55%  12-16-14 17:35  c7cb7a03  META-INF/MANIFEST.MF
    1598  Defl:N      724  55%  12-16-14 17:35  034c0dfc  META-INF/CERT.SF
     776  Defl:N      605  22%  12-16-14 17:35  2be8b27c  META-INF/CERT.RSA
--------          -------  ---                            -------
 1565085           849785  46%                            21 files
  1. Re-sign the package, manually, with jarsigner. The intent of this is to ensure that the META-INF entries are at the beginning of the .apk:

    $ yes android | keytool -genkey -v -keystore demo.keystore -alias demo -keyalg RSA -keysize 2048 -validity 10000 -dname "CN=Mark Smith" $ cp Application/build/outputs/apk/Application-debug.apk A.apk $ zip -d A.apk META-INF/MANIFEST.MF $ zip -d A.apk META-INF/CERT.SF $ zip -d A.apk META-INF/CERT.RSA $ jarsigner -keystore demo.keystore -storepass android \ -keypass android -digestalg SHA1 -sigalg md5withRSA \ -signedjar A-unaligned.apk A.apk \ demo $ zipalign 4 A-unaligned.apk A-aligned.apk

  2. Install the re-signed package:

    $ adb uninstall com.example.android.pdfrendererbasic $ adb install A-aligned.apk

  3. Run the new package:

    $ adb shell am start com.example.android.pdfrendererbasic/.MainActivity

like image 43
Jakub Petriska Avatar answered Nov 03 '22 00:11

Jakub Petriska