在android应用程序中显示pdf
在我的应用程序中,在android应用程序中显示pdf,android,pdf,Android,Pdf,在我的应用程序中,resource/raw/color\u chart\u ciao.PDF中有一个PDF文件。我想在我的应用程序中显示该文件。我已经为此编写了代码: 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()
resource/raw/color\u chart\u ciao.PDF
中有一个PDF文件。我想在我的应用程序中显示该文件。我已经为此编写了代码:
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。
我在应用程序开发方面比较新鲜。因此,请帮助回答这个问题。如果您在设备/模拟器中安装了任何默认的PDF阅读器,那么只有您可以通过intent打开PDF,但您的应用程序将不是一个独立的应用程序。如果你想让你的应用程序独立,那么你必须在你的应用程序中实现PDF阅读器。在PDF上有开源项目,您可以阅读 以下是PDF的几个链接:
-在活动中复制以下代码。在任何需要的地方调用函数copyreadsets(“File_name.pdf”)。将文件_name.pdf文件放在资产文件夹中。会有用的!快乐编码!:) }
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();
}