Android - 从资产 PDF 显示访问文件 [英] Android - Access file from assets PDF display
问题描述
我正在尝试将存储在 assets
目录中的文件的引用检索到名为 myfile.pdf
的文件.我尝试这样做:
I am trying to retrieve a reference to a file stored in the assets
directory to a file named myfile.pdf
. I have tried to do it as follows:
File file = new File("android_assest/myfile.pdf);
Log.d("myTag", "" + file.isFile());
不知何故,当 myfile.pdf
确实存在于 assets
目录中时,我得到 false
.我使用 getAssets().list("")
和 Log.d()
对返回数组中的每个元素进行了验证.
Somehow, I get false
when the myfile.pdf
do exists in the assets
directory. I verified it using getAssets().list("")
and Log.d()
each element in the returned array.
更多的是,我试图获取对 PDF 文件的引用,然后使用设备上已安装的任何 PDF 查看器来查看 PDF.
More of which, I am trying to get a reference to a PDF file and then use any PDF viewer, which is already installed on the device, in order to view the PDF.
我猜因为上一个问题(检索对文件的引用)返回 false
那么下一个截取的代码失败了:
I guess that since the previous issue (retrieving a reference to the file) returns false
then the next snipped code fails:
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("file:///android_asset/myfile.pdf"));
startActivity(i);
有人知道为什么我无法检索到文件的引用吗?以及为什么我无法使用已安装的 PDF 查看器来显示 PDF(在检索对 PDF 文件的引用后)?
Anyone has a clue why I am unable to retrieve a reference to the file? and why I cannot use already installed PDF viewer to display a PDF (after retrieving a reference to the PDF file)?
谢谢.
推荐答案
正如 Barak 所说,您可以将其从资产复制到内部存储或 SD 卡,然后使用内置的 pdf 应用程序从那里打开它.
As Barak said you can copy it out of assets to internal storage or the SD card and open it from there using inbuilt pdf applications.
遵循 Snippet 将对您有所帮助.(我已更新此代码以写入和读取内部存储中的文件.
Following Snippet will help you. (I have updated this code to write to and read files from internal storage.
但我不推荐这种方法,因为 pdf 文件的大小可能超过 100mb.
But i dont recommend this approach because pdf file can be more than 100mb in size.
所以不建议将那个大文件保存到内部存储中
So its not recommended to save that huge file into internal storage
还要确保在将文件保存到您使用的内部存储器时
Also make sure while saving file to internal storage you use
openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
然后只有其他应用程序可以读取它.
Then only other applications can read it.
检查以下代码段.
package org.sample;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
public class SampleActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CopyReadAssets();
}
private void CopyReadAssets()
{
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "git.pdf");
try
{
in = assetManager.open("git.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() + "/git.pdf"),
"application/pdf");
startActivity(intent);
}
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);
}
}
}
确保包含
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在清单中
这篇关于Android - 从资产 PDF 显示访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!