I am making an application which require to open pdf.
I also have some pdf in asset folder so i am not able to open it in webview directly.
By default android does not support pdf.
Is there any API that works on android(except MuPdf) ??
My device does not have any pdf reader installed so ACTION VIEW is not helpful for me
Following is not working.......
How to render a PDF file in Android
Open asset file pdf in application
Can you suggest me any good api?
Adobe Acrobat automatically recovers all unsaved PDF files when the application is restarted after a crash. Just select the file and it will be recovered. Open your unsaved PDF file on which you were working when the application crashed. Click Yes when prompted to open the file saved with the Autosave feature.
The very first and the easiest way of displaying the PDF file is to display it in the WebView. All you need to do is just put WebView in your layout and load the desired URL by using the webView. loadUrl() function. Now, run the application on your mobile phone and the PDF will be displayed on the screen.
To fix a PDF file not opening in Adobe reader, you will need to download the latest version of Adobe Reader. After which you will disable the protected mode that comes with it by default. Once this is changed, the issue of the PDF file not opening in Adobe reader will be resolved.
How do I change the default 'open with' on Android? Go to Settings > Apps > Select the default open with app > Clear defaults. Now, the next time you open a PDF file, select the app you would like to set as default 'open with' and select Remember my choice or Always option.
I've simply done that using PdfViewer.jar (download it with the blue button) and making a code like below.
First.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); File images = Environment.getExternalStorageDirectory(); imagelist = images.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return ((name.endsWith(".pdf"))); } }); pdflist = new String[imagelist.length]; for(int i = 0;i<imagelist.length;i++) { pdflist[i] = imagelist[i].getName(); } this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, pdflist)); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); String path = imagelist[(int)id].getAbsolutePath(); openPdfIntent(path); } private void openPdfIntent(String path) { try { final Intent intent = new Intent(First.this, Second.class); intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path); startActivity(intent); } catch (Exception e) { e.printStackTrace(); } }
Second.java
public class Second extends PdfViewerActivity { @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } public int getPreviousPageImageResource() { return R.drawable.left_arrow; } public int getNextPageImageResource() { return R.drawable.right_arrow; } public int getZoomInImageResource() { return R.drawable.zoom_in; } public int getZoomOutImageResource() { return R.drawable.zoom_out; } public int getPdfPasswordLayoutResource() { return R.layout.pdf_file_password; } public int getPdfPageNumberResource() { return R.layout.dialog_pagenumber; } public int getPdfPasswordEditField() { return R.id.etPassword; } public int getPdfPasswordOkButton() { return R.id.btOK; } public int getPdfPasswordExitButton() { return R.id.btExit; } public int getPdfPageNumberEditField() { return R.id.pagenum_edit; } }
Hope this helps you lot. Try this. Don't forgot to add your Second.java
in your manifest. Add some drawables whatever it requires in second.java
with your drawables. And, Refer the example from GitHub
Some phones (like the Nexus One) come with a version of Quickoffice pre-installed so it may be as easy as sending the appropriate Intent once you've saved the file to the SD card.
public class OpenPdf extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.OpenPdfButton); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { File file = new File("/sdcard/example.pdf"); if (file.exists()) { Uri path = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(path, "application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); try { startActivity(intent); } catch (ActivityNotFoundException e) { Toast.makeText(OpenPdf.this, "No Application Available to View PDF", Toast.LENGTH_SHORT).show(); } } } }); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With