在android应用展示PDF [英] show pdf in android application
问题描述
我在资源/生/我application.I color_chart_ciao.pdf
PDF文件要显示在我的应用程序文件。我已经写了code为:
I have a PDF file in resource/raw/color_chart_ciao.pdf
in my application.I want to show that file in my application. I have written code for that:
File file = new File("http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf");
System.out.println("FIle Path is" + file);
if (file.exists()) {
System.out.println("FIle Path is" + file);
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
System.out.println("pdf show");
}
catch (ActivityNotFoundException e) {
Toast.makeText(CiaoView.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
但我不能看到PDF在我的应用程序,当我运行我的应用程序。 我公司在应用程序开发更新鲜。所以,请帮助这个问题。
but I can't see the PDF on my application when i run my application. I am fresher in application development. So please help with this question.
推荐答案
- 复制以下code。在您的活动。调用函数CopyReadAssets(File_name.pdf),从那里过你想要的。将资产文件夹中的File_name.pdf文件。它会工作!编码快乐! :)
-Copy the following code in your activity. Call the function CopyReadAssets("File_name.pdf") from where ever you want. Place the File_name.pdf file in assets folder. It'll work! Happy Coding! :)
private void CopyReadAssets(String pdfname)
{
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), pdfname);
try
{
in = assetManager.open(pdfname);
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{
Toast.makeText(getApplicationContext(), "Pdf Viewer not installed", Toast.LENGTH_SHORT).show();
}
try
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/"+pdfname),
"application/pdf");
startActivity(intent);
}catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(), "Pdf Viewer not installed" ,Toast.LENGTH_SHORT).show();
}
}
这篇关于在android应用展示PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!