我正在使用 API Level 21,我正在尝试显示从 Internet 下载并存储在缓存目录中的 PDF。
private void openRenderer(Context context) throws IOException {
Log.d("NoiPA", "Opening PDF File: " + pdfPath);
// In this sample, we read a PDF from the assets directory.
mFileDescriptor = ParcelFileDescriptor.open(new File(pdfPath),ParcelFileDescriptor.MODE_READ_ONLY);
//mFileDescriptor = context.getAssets().openFd(pdfPath).getParcelFileDescriptor();
// This is the PdfRenderer we use to render the PDF.
mPdfRenderer = new PdfRenderer(mFileDescriptor);
}
上面的代码使用以下堆栈跟踪使应用程序崩溃:
Download: Completed -> Showing PDF
onAttach -> pdfPath: /data/data/com.vitobellini.noipa/cache/MAG 2016.pdf
Opening PDF File: /data/data/com.vitobellini.noipa/cache/MAG 2016.pdf
java.io.IOException: not create document. Error:
at android.graphics.pdf.PdfRenderer.nativeCreate(Native Method)
at android.graphics.pdf.PdfRenderer.<init>(PdfRenderer.java:149)
at com.vitobellini.noipa.PdfRendererFragment.openRenderer(PdfRendererFragment.java:151)
at com.vitobellini.noipa.PdfRendererFragment.onAttach(PdfRendererFragment.java:115)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1019)
at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:779)
at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:819)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:660)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我无法向自己解释为什么该代码不起作用,有人知道如何修复它吗?