1

After reading through several Q/A, I still can't find a suitable answer for my current issue.

I have a pdf-file (known at compile time) which is stored in my /res/raw folder.

I have tried loading the file using:

InputStream is = getResources().openRawResource(R.raw.mypdf);

Then I want to display the pdf (in an intent) using the preferred pdf-reader on the device:

Intent i;
i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(file,"application/pdf");
startActivity(i);

The issue is that the intent takes in the type 'File', while my pdf is read as an 'InputStream'.

The question is: How can i display the pdf-file? i.e. how can I display an InputStream? or how can I store the pdf-file to allow opening with new File()?

1

2 Answers 2

1

TRY this..
//place pdf in asset folder just to try

Uri file= Uri.parse("file:///android_asset/mypdf.pdf");
  String mimeType =  MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(file.toString()));

try{
     Intent i;
     i = new Intent(Intent.ACTION_VIEW);
     i.setDataAndType(file,mimeType);
     startActivity(i);

}catch (ActivityNotFoundException e) {
                    Toast.makeText(this, 
                        "No Application Available to fiew this file type", 
                        Toast.LENGTH_SHORT).show();
                } 
Sign up to request clarification or add additional context in comments.

11 Comments

It throws an ActivityNotFoundException. Furthermore the String mimeType is blank (using Toast). It seems like the extension isn't recognized.
Okay... Problem is there is no application in your device to read a pdf file.. Install adobe reader and try again...
did you try to display mimetype?
I have adobe reader, but the actual string returned in mimeType (2nd line in your code) is blank, so the intent is called as i.setDataAndType(file,""). But is this an issue with the file/storage method or the MimeTypeMap function on a resource?
getMimeTypeFromExtension(".pdf")
|
1

you could use an extern library joanzapata.pdfview

this code will show your pdf whereever u want in your layout

 

    private PDFView pdfview;
    pdfview = (PDFView) findViewById(R.id.pdfview);
    File file = new File(filepath);
    pdfview.fromFile(file)
                    .defaultPage(1)
                    .showMinimap(false)
                    .enableSwipe(true)
                    .onLoad(this)
                    .onPageChange(this)
                    .load();


or if the file is known at compile time and u have the pdf in the asset folder:

 

    private PDFView pdfview;
    pdfview = (PDFView) findViewById(R.id.pdfview);
    pdfview.fromAsset(pdfName)
                    .defaultPage(1)
                    .showMinimap(false)
                    .enableSwipe(true)
                    .onLoad(this)
                    .onPageChange(this)
                    .load();


add this in your layout

<com.joanzapata.pdfview.PDFView
                android:id="@+id/pdfview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.