1

I am trying to read a PDF file name main.pdf from raw folder and then open it on an ImageView. It's showing no error, but the bitmap is returning null. The main purpose of my application is to open only one PDF file which will be embedded in the application, and users can read only this PDF file. If there is another solution, you can suggest me.

private void read()
{
    InputStream in;
    try
    {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        in = getResources().openRawResource(R.raw.main);
        System.out.println("input stream = " + in);

        BufferedInputStream buffer = new BufferedInputStream(in);
        System.out.println("buffer stream = " + buffer);
        Bitmap bitmap = BitmapFactory.decodeStream(buffer, null, options);
        System.out.println("bitmap = " + bitmap);

        Matrix imageMatrix = image.getImageMatrix();
        image.setImageMatrix(imageMatrix);
        image.setImageBitmap(bitmap);
        image.invalidate();
        in.close();
    } catch (Exception e)
    {
        Log.e("tag", e.getMessage());
        System.err.print(e);
    }
}
SmokeDetector
  • 751
  • 2
  • 8
  • 12

1 Answers1

0

Like what @greenapps has said, BitmapFactory cannot open PDF files.

What exactly are you trying to accomplish? Is your app primarily a PDF reader app, or is PDF reading a minor feature?

If it's the latter, I suggest that you look into 3rd party libraries such as AndroidPdfViewer or AndroidPdfViewerV1 to view PDF files in your app.

Noel Bautista
  • 175
  • 1
  • 12