【发布时间】:2013-02-12 19:22:19
【问题描述】:
我的资产中存储了一个 PDF 文件。我想从我的资产加载 PDF 并在应用程序本身中阅读它,而不使用任何 3rd 方应用程序来查看。
我在link 中得到了解决方案。从 sdcard 中选择文件时效果很好。
【问题讨论】:
-
您是否考虑过将文件从 assets 文件夹复制到 SD 卡?
-
检查this!一个
我的资产中存储了一个 PDF 文件。我想从我的资产加载 PDF 并在应用程序本身中阅读它,而不使用任何 3rd 方应用程序来查看。
我在link 中得到了解决方案。从 sdcard 中选择文件时效果很好。
【问题讨论】:
以下 sn-p 可能会帮助您从asset 文件夹访问文件然后打开它:
private void ReadFromAssets()
{
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "file.pdf");
try
{
in = assetManager.open("file.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{
Log.e("tag", e.getMessage());
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/file.pdf"),
"application/pdf");
startActivity(intent);
}
和copyFile方法如下:
private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}
编辑
为此,您必须使用外部库。在下面的链接中解释得很好: Render a PDF file using Java on Android
希望这会对你有所帮助。
【讨论】:
如果你能用webview打开它就更好了
WebView web = (WebView) findViewById(R.id.webView1);
web.loadUrl("file:///android_asset/yourpdf.pdf");
希望它有效。
糟糕,我刚刚检查了,pdf 无法在 web 视图中加载 对不起
【讨论】: