1. Are you ready for the Galaxy S20? Here is everything we know so far!

PDF Renderer

Discussion in 'Android Development' started by TheSnowmann, Jun 27, 2017.

  1. TheSnowmann

    TheSnowmann Lurker
    Thread Starter

    Hey, I am super new to programming but I am trying to learn through the process of making apps for the android. I am trying to make a application that will have PDFs stored in a asset folder within the app and activities will call upon and render those pdfs. But I got stuck and I can't solve this issue. What am I doing wrong, teach me!

    MainActivity.java
    Code (Text):
    1.  
    2. package net.development.walkthroughs;
    3.  
    4. import android.graphics.Bitmap;
    5. import android.graphics.Matrix;
    6. import android.graphics.Rect;
    7. import android.graphics.pdf.PdfRenderer;
    8. import android.os.Bundle;
    9. import android.os.Environment;
    10. import android.os.ParcelFileDescriptor;
    11. import android.support.v7.app.AppCompatActivity;
    12. import android.view.View;
    13. import android.widget.Button;
    14. import android.widget.ImageView;
    15.  
    16. import java.io.File;
    17.  
    18. public class MainActivity extends AppCompatActivity {
    19.  
    20.     private ImageView imageView;
    21.     private int currentPage = 0;
    22.     private Button next, previous;
    23.  
    24.     @Override
    25.     protected void onCreate(Bundle savedInstanceState) {
    26.         super.onCreate(savedInstanceState);
    27.         setContentView(R.layout.activity_main);
    28.  
    29.         next = (Button) findViewById(R.id.next);
    30.         previous = (Button) findViewById(R.id.previous);
    31.  
    32.         next.setOnClickListener(new View.OnClickListener() {
    33.             @Override
    34.             public void onClick(View view) {
    35.                 currentPage++;
    36.                 render();
    37.             }
    38.         });
    39.  
    40.         previous.setOnClickListener(new View.OnClickListener() {
    41.             @Override
    42.             public void onClick(View view) {
    43.                 currentPage--;
    44.                 render();
    45.             }
    46.         });
    47.  
    48.         render();
    49.     }
    50.  
    51.     private void render() {
    52.         try {
    53.             imageView = (ImageView) findViewById(R.id.image);
    54.             int REQ_WIDTH = imageView.getWidth();
    55.             int REQ_HEIGHT = imageView.getHeight();
    56.  
    57.             Bitmap bitmap = Bitmap.createBitmap(REQ_WIDTH, REQ_HEIGHT, Bitmap.Config.ARGB_4444);
    58.             File file = new File(Environment.getDataDirectory().getPath()+"assets/1.pdf");
    59.             PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY));
    60.  
    61.             if(currentPage < 0) {
    62.                 currentPage = 0;
    63.             } else if (currentPage > renderer.getPageCount()) {
    64.                 currentPage = renderer.getPageCount() - 1;
    65.             }
    66.  
    67.             Matrix m = imageView.getImageMatrix();
    68.             Rect rect = new Rect(0, 0, REQ_WIDTH, REQ_HEIGHT);
    69.             renderer.openPage(currentPage).render(bitmap, rect, m, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
    70.             imageView.setImageMatrix(m);
    71.             imageView.setImageBitmap(bitmap);
    72.             imageView.invalidate();
    73.  
    74.         } catch (Exception e) {
    75.             e.printStackTrace();
    76.         }
    77.     }
    78. }
    activity_main.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     xmlns:app="http://schemas.android.com/apk/res-auto"
    5.     xmlns:tools="http://schemas.android.com/tools"
    6.     android:layout_width="match_parent"
    7.     android:layout_height="match_parent"
    8.     tools:context="net.development.walkthroughs.MainActivity"
    9.     android:orientation="vertical">
    10.  
    11.     <ImageView
    12.         android:id="@+id/image"
    13.         android:layout_width="match_parent"
    14.         android:layout_height="0dp"
    15.         android:scaleType="fitCenter"/>
    16.  
    17.     <LinearLayout
    18.         android:layout_width="match_parent"
    19.         android:layout_height="match_parent">
    20.      
    21.         <Button
    22.             android:id="@+id/previous"
    23.             android:layout_width="0dp"
    24.             android:layout_height="wrap_content"
    25.             android:layout_weight="1"
    26.             android:text="  Previous Page"/>
    27.  
    28.         <Button
    29.             android:id="@+id/next"
    30.             android:layout_width="0dp"
    31.             android:layout_height="wrap_content"
    32.             android:layout_weight="1"
    33.             android:text="Next Page "/>
    34.     </LinearLayout>
    35.  
    36. </LinearLayout>
    AndroidManifest.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    4.     package="net.development.walkthroughs">
    5.  
    6.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    7.  
    8.     <application
    9.         android:allowBackup="true"
    10.         android:icon="@mipmap/ic_launcher"
    11.         android:label="@string/app_name"
    12.         android:roundIcon="@mipmap/ic_launcher_round"
    13.         android:supportsRtl="true"
    14.         android:theme="@style/AppTheme">
    15.         <activity android:name=".MainActivity">
    16.             <intent-filter>
    17.                 <action android:name="android.intent.action.MAIN" />
    18.  
    19.                 <category android:name="android.intent.category.LAUNCHER" />
    20.             </intent-filter>
    21.         </activity>
    22.     </application>
    23.  
    24. </manifest>
    Honestly, I think my issue lies in this part of the render private void. Specifically the "assets/1.pdf"

    MainActivity.java
    Code (Text):
    1.  
    2. private void render() {
    3.     try {
    4.         imageView = (ImageView) findViewById(R.id.image);
    5.         int REQ_WIDTH = imageView.getWidth();
    6.         int REQ_HEIGHT = imageView.getHeight();
    7.  
    8.         Bitmap bitmap = Bitmap.createBitmap(REQ_WIDTH, REQ_HEIGHT, Bitmap.Config.ARGB_4444);
    9.         File file = new File(Environment.getDataDirectory().getPath()+"assets/1.pdf");
    10.         PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY));
    Also, I created the assets directory and copied over the 1.pdf to it.

    Thank you for your assistance.

    -Snow

     


    moufoumafulgence90 likes this.
  2. Best Answer:
    Post #4 by Deleted User, Jun 28, 2017 (3 points)

    1. Download the Forums for Android™ app!


      Download

       
  3. Deleted User

    Deleted User Guest

    Hi. What's the issue you are having with this code?
     
  4. TheSnowmann

    TheSnowmann Lurker
    Thread Starter

    Oh I am sorry, I didn't specify. Its not displaying the pdf file I have placed in the assets folder of the app
     
  5. Deleted User

    Deleted User Guest

    Do you get any exceptions in the Logcat? Does the application crash?

    Run your app in debug mode, and set a breakpoint in method render(). Step through the code line by line and see what happens.
     
  6. TheSnowmann

    TheSnowmann Lurker
    Thread Starter

    I don't know how to do any of that. But I shall find out and report back my findings.
     
  7. Deleted User

    Deleted User Guest

    Good plan. Learning to debug your app is *the* most useful skill to acquire, if you're developing apps.
     
Loading...

Share This Page

Loading...