问题
I'm using this github 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 this issue 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?
回答1:
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.
How can I work around it?
Either:
Follow the instructions in that issue and copy the asset to a file, or
Modify that library to support an asset, or
Do not use that library, but use something else that does support displaying a PDF from an asset
FWIW, I have sample apps demonstrating the use of pdf.js and pdfium for displaying PDFs from assets.
来源:https://stackoverflow.com/questions/48948625/not-able-to-open-pdf-in-new-activity