如何从Android的资产文件夹中打开PDF文件? [英] How to open PDF file in Android from the assets folder?
本文介绍了如何从Android的资产文件夹中打开PDF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
没有任何一个对如何在Android中打开PDF文件的想法?我的code相本这样的:
公共类SampleActivity延伸活动{ @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
CopyReadAssets();
} 私人无效CopyReadAssets(){
AssetManager assetManager = getAssets(); 在的InputStream = NULL;
出的OutputStream = NULL;
档案文件=新的文件(getFilesDir(),git.pdf);
尝试{
在= assetManager.open(git.pdf);
OUT = openFileOutput(file.getName(),Context.MODE_WORLD_READABLE); copyFile(IN,OUT);
附寄();
在= NULL;
了out.flush();
out.close();
出= NULL;
}赶上(例外五){
Log.e(标签,e.getMessage());
} 意向意图=新意图(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse(文件://+ getFilesDir()+/git.pdf),
应用程序/ PDF格式); startActivity(意向);
} 私人无效copyFile(在的InputStream,OutputStream的了)抛出IOException
字节[]缓冲区=新的字节[1024];
INT读;
而((读= in.read(缓冲))!= -1){
out.write(缓冲,0,读);
}
}
}
解决方案
下面是code从资产的文件夹中打开PDF文件,但你必须有你的设备上安装PDF阅读器:
私人无效CopyAssets(){ AssetManager assetManager = getAssets(); 在的InputStream = NULL;
出的OutputStream = NULL;
档案文件=新的文件(getFilesDir(),fileName.pdf);
尝试{
在= assetManager.open(fileName.pdf);
OUT = openFileOutput(file.getName(),Context.MODE_WORLD_READABLE); copyFile(IN,OUT);
附寄();
在= NULL;
了out.flush();
out.close();
出= NULL;
}赶上(例外五){
Log.e(标签,e.getMessage());
} 意向意图=新意图(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse(文件://+ getFilesDir()+/fileName.pdf),
应用程序/ PDF格式); startActivity(意向);
} 私人无效copyFile(在的InputStream,OutputStream的了)抛出IOException
字节[]缓冲区=新的字节[1024];
INT读;
而((读= in.read(缓冲))!= -1){
out.write(缓冲,0,读);
}
}
Does any one has an idea on how to open a PDF file in Android? My code looks this this:
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);
}
}
}
解决方案
Here is the code for opening pdf file from asset folder, but you must have pdf reader installed on your device :
private void CopyAssets() {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "fileName.pdf");
try {
in = assetManager.open("fileName.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() + "/fileName.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);
}
}
这篇关于如何从Android的资产文件夹中打开PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文