无法在新活动中打开 pdf [英] Not able to open pdf in new activity
问题描述
我正在为我的项目使用 这个 github 源代码.我在 assets
文件夹中有一个包含 10 个元素和 10 个不同 PDF 的列表视图.在 1 个活动中,我使用 listview
和 onItemClickListener
如下所示:
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);
}
});
PdfViewerActivity
类的代码如下:
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;
}
}
当我单击列表视图中的任何项目时,应打开新活动,并应在新活动中查看 PDF.当我运行我的代码时,我总是收到这个错误:
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'
我在 github 存储库中遇到了这个这个问题问题.它说无法从 Assets 文件夹加载 PDF.您应该在打开之前将其复制到 SD 卡上.我该如何解决?
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?
推荐答案
intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, getAssets() + "/dummy.pdf");
getAssets()
返回一个 AssetManager
.getAssets() + "/dummy.pdf"
在 AssetManager
实例上调用 toString()
,然后附加 /dummy.pdf
到它.这就是为什么你会得到 android.content.res.AssetManager@817d40c/dummy.pdf
.AssetManager
上的 toString()
不会以某种方式神奇地为您提供资产的文件系统路径...部分原因是资产不是设备上的文件.
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.
我该如何解决?
要么:
按照该问题中的说明将资产复制到文件中,或者
Follow the instructions in that issue and copy the asset to a file, or
修改该库以支持资产,或
Modify that library to support an asset, or
不要使用那个库,而是使用其他支持从资产显示 PDF 的东西
Do not use that library, but use something else that does support displaying a PDF from an asset
FWIW,我有示例应用程序演示了 <代码>pdf.js 和 pdfium 用于显示来自资产的 PDF.
FWIW, I have sample apps demonstrating the use of pdf.js
and pdfium for displaying PDFs from assets.
这篇关于无法在新活动中打开 pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!