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()?

ThomasKJDK
  • 367
  • 1
  • 7
  • 17

2 Answers2

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();
                } 
vnshetty
  • 20,051
  • 23
  • 64
  • 102
  • It throws an ActivityNotFoundException. Furthermore the String mimeType is blank (using Toast). It seems like the extension isn't recognized. – ThomasKJDK Apr 12 '12 at 12:26
  • Okay... Problem is there is no application in your device to read a pdf file.. Install adobe reader and try again... – vnshetty Apr 12 '12 at 12:28
  • did you try to display mimetype? – vnshetty Apr 12 '12 at 12:29
  • 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? – ThomasKJDK Apr 12 '12 at 12:29
  • getMimeTypeFromExtension(".pdf") – vnshetty Apr 12 '12 at 12:32
  • hey i edited the code can you just place that file in asset folder and try my code? because its not taking a proper mimetype. is that your requirment to place it in raw folder? – vnshetty Apr 12 '12 at 12:36
  • It now seems like it attempts to open the pdf-reader, but the app 'force quit' before anything is displayed. I'm concerned that the file is not recognized as .pdf when it is accessed using Uri.parse(). – ThomasKJDK Apr 12 '12 at 12:41
  • In now prompt me to choose between Adobe/QuickOffice, but gives the error: "The document path is not valid." (It does NOT throw the ActivityNotFoundException) – ThomasKJDK Apr 12 '12 at 12:45
  • see this thread http://stackoverflow.com/questions/6491210/how-to-open-a-pdf-stored-either-in-res-raw-or-assets-folder – vnshetty Apr 13 '12 at 06:49
  • I ended up putting the files online, and then downloading it to external storage before viewing. Thanks for you help anyways! – ThomasKJDK Apr 16 '12 at 02:19
  • Try saving the pdf file using the InputStream 'is' to the external storage and then create a Uri to that file like: file = new File("pathtofile"); Uri uri = Uri.fromFile(file); intent.setDataAndType(uri, mimeType); – Larphoid May 04 '12 at 13:48
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"/>
Jotunheim
  • 110
  • 1
  • 11