Search code examples
javaandroidandroid-fragmentsandroid-intentpdfrenderer

How to render a pdf in android using pdf renderer


Hello guys how do solve this my error log I'm trying to render pdfs using the android graphics pdfrenderer class in android I've tried to observe where or how I got it wrong but I can't see yet please guys how exactly do I achieve this using this particular class any idea will be very much appreciated.


Shared attribute region not available to be mapped
02-04 05:35:25.750 15257 15257 W   ActivityThread                               handleWindowVisibility: no activity for token android.os.BinderProxy@cb509ee
02-04 05:35:25.936 15257 15257 W   Bundle                                       Key filePath expected String but value was a java.io.File.  The default value <null> was returned.
02-04 05:35:25.950 15257 15257 W   Bundle                                       Attempt to cast generated internal exception:
02-04 05:35:25.950 15257 15257 W   Bundle                                       java.lang.ClassCastException: java.io.File cannot be cast to java.lang.String
02-04 05:35:25.950 15257 15257 W   Bundle                                       at android.os.BaseBundle.getString(BaseBundle.java:1170)
02-04 05:35:25.950 15257 15257 W   Bundle                                       at android.content.Intent.getStringExtra(Intent.java:7907)
02-04 05:35:25.950 15257 15257 W   Bundle                                       at com.jaay.docReader.PdfActivity.onCreate(PdfActivity.java:26)
02-04 05:35:25.950 15257 15257 W   Bundle                                       at android.app.Activity.performCreate(Activity.java:7873)
02-04 05:35:25.950 15257 15257 W   Bundle                                       at android.app.Activity.performCreate(Activity.java:7861)
02-04 05:35:25.950 15257 15257 W   Bundle                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1312)
02-04 05:35:25.950 15257 15257 W   Bundle                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3331)
02-04 05:35:25.950 15257 15257 W   Bundle                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3533)
02-04 05:35:25.950 15257 15257 W   Bundle                                       at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
02-04 05:35:25.950 15257 15257 W   Bundle                                       at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)

This the activity handling the pdf activity and it's display


public class PdfActivity extends AppCompatActivity {
    
    String filePath = "";
    ImageView pdfView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdf);
        pdfView = (ImageView) findViewById(R.id.pdfview);
        filePath = getIntent().getStringExtra("filePath");
        File file = new File(filePath);
        try{
            openPDF(file);
        }catch(IOException ioe){
            ioe.printStackTrace();
        }
        
        //File file = new File(filePath);
        //Uri uri = Uri.fromFile(file);
        //pdfView.fromUri(uri).load();
        
        
    }
    
    
    public void openPDF(File file) throws IOException {
       // File file = new File(filePath);

        ParcelFileDescriptor fileDescriptor = null;
        fileDescriptor = ParcelFileDescriptor.open(
            file, ParcelFileDescriptor.MODE_READ_ONLY);

        //min. API Level 21
        PdfRenderer pdfRenderer = null;
        pdfRenderer = new PdfRenderer(fileDescriptor);

        final int pageCount = pdfRenderer.getPageCount();
        Toast.makeText(this,
                       "pageCount = " + pageCount,
                       Toast.LENGTH_LONG).show();

        //Display page 0
        PdfRenderer.Page rendererPage = pdfRenderer.openPage(0);
        int rendererPageWidth = rendererPage.getWidth();
        int rendererPageHeight = rendererPage.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(
            rendererPageWidth,
            rendererPageHeight,
            Bitmap.Config.ARGB_8888);
        rendererPage.render(bitmap, null, null,
                            PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);

        pdfView.setImageBitmap(bitmap);
        rendererPage.close();

        pdfRenderer.close();
        fileDescriptor.close();
    }
    
    
    
}

This is the code passing the pdf files using intent from fragment to activity.

startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsoluteFile()));

Here is the pdf activity 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:id="@+id/activity_pdf"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="jaa"
        android:textStyle="bold"/>

    <ImageView
        android:id="@+id/pdfview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    
</LinearLayout>

Where did I go wrong and what's odd in

filePath = getIntent().getStringExtra("filePath");
        File file = new File(filePath);

Solution

  • startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsoluteFile()));

    Change to:

    startActivity(new Intent(getActivity(), PdfActivity.class).putExtra("filePath", file.getAbsolutePath()));