66721

Not able to open pdf in new activity

Question:

I'm using <a href="https://github.com/jblough/Android-Pdf-Viewer-Library" rel="nofollow">this github</a> source for my project. I've a listview with 10 elements and 10 different PDFs in assets folder. In 1 activity, I'm using listview and onItemClickListener as shown below:

ArrayAdapter arrayAdapter = new ArrayAdapter<>(this, R.layout.activity_policy_list, mobileArray); ListView listView = (ListView) findViewById(R.id.policy_list); listView.setAdapter(arrayAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Intent intent = new Intent(PolicyActivity.this, PdfViewerActivity.class); intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, getAssets() + "/dummy.pdf"); startActivity(intent); } });

Code for PdfViewerActivity class is as given below:

package com.dell.eventapp.ui.activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.dell.eventapp.R; public class PdfViewerActivity extends net.sf.andpdf.pdfviewer.PdfViewerActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pdf_viewer); } @Override public int getPreviousPageImageResource() { return R.drawable.left_arrow; } @Override public int getNextPageImageResource() { return R.drawable.right_arrow; } @Override public int getZoomInImageResource() { return R.drawable.zoom_in; } @Override public int getZoomOutImageResource() { return R.drawable.zoom_out; } @Override public int getPdfPasswordLayoutResource() { return 0; } @Override public int getPdfPageNumberResource() { return 0; } @Override public int getPdfPasswordEditField() { return 0; } @Override public int getPdfPasswordOkButton() { return 0; } @Override public int getPdfPasswordExitButton() { return 0; } @Override public int getPdfPageNumberEditField() { return 0; } }

When I click on any item in list view, new activity should open up and PDF should be viewed in new activity. When I run my code, I always get this error:

02-23 18:19:48.033 8982-8982/com.dell.eventapp I/PDFVIEWER: ST='file 'android.content.res.AssetManager@817d40c/dummy.pdf' not found'

I came across this <a href="https://github.com/jblough/Android-Pdf-Viewer-Library/issues/26" rel="nofollow">this issue</a> in github repo's issues. It says that PDF cannot be loaded from Assets folder. You should copy it on sdcard before opening it. How can I work around it?

Answer1:

intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, getAssets() + "/dummy.pdf");

getAssets() returns an AssetManager. getAssets() + "/dummy.pdf" calls toString() on the AssetManager instance, and then appends /dummy.pdf to it. This is why you wind up with android.content.res.AssetManager@817d40c/dummy.pdf. toString() on AssetManager does not somehow magically give you a filesystem path to an asset... in part because assets are not files on the device.

<blockquote>

How can I work around it?

</blockquote>

Either:

<ul><li>

Follow the instructions in that issue and copy the asset to a file, or

</li> <li>

Modify that library to support an asset, or

</li> <li>

Do not use that library, but use something else that does support displaying a PDF from an asset

</li> </ul>

FWIW, I have sample apps demonstrating the use of <a href="https://github.com/commonsguy/cw-omnibus/tree/v8.9/PDF/PdfJS" rel="nofollow">pdf.js</a> and <a href="https://github.com/commonsguy/cw-omnibus/tree/v8.9/PDF/Pdfium" rel="nofollow">pdfium</a> for displaying PDFs from assets.

Recommend

  • I am having Directx 11 but WDDM 1.0 ? how to update it to WDDM 1.1? [duplicate]
  • how to use font file
  • visual studio fatal error LNK1120:
  • Andengine loading graphics: why is my background texture small and upside down
  • Saving image file path to sqlite database
  • NHibernate Lazy Loading Behaviour
  • How to know whan a speceific bluetooth device is connecting?
  • Running a Service from ASyncTask or Thread?
  • Testing Rails module without loading ActiveRecord
  • How to get Confirmation of email send or discarded in android
  • Intent Image Capture data==null
  • Font Awesome in Viewpager Previous Next
  • How to Update model and database with code first approach in Asp.net MVC
  • FCM Data messages are not working properly
  • How to call BeanFactoryPostProcessor.postProcessBeanFactory method when use Spring with XML configur
  • How to move again MainActivity after sending the mail?
  • Android - Call default browser with and redirect to a designated url
  • Receive custom intent without activity restart
  • Android NFC read Tags issue. Activity starts each time on data received
  • Send data from edittext to listview
  • How to display the images in listview
  • Why does the font in these TD elements render at different sizes?
  • Get the number 18437736874454810627
  • Splash Screen will not display
  • Is there a way to dynamically embed PDF Files in a JSP pulled from the file system?
  • How can go in last activity with resume button?
  • Creating PDF from TIFF image using iText
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • Read text file that is not in the main package in a runnable jar
  • Roxygen error “Skipping invalid path”
  • Android Google Maps API v2 start navigation
  • Android application: how to use the camera and grab the image bytes?
  • Dialing with Intent.ACTION_CALL stopps at # in phone number
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • jQuery tmpl and DataLink beta
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal