简体   繁体   English

如何从android studio中的res文件夹中读取pdf文件

[英]how to read pdf files from res folder in a android studio

Can anyone help me. 谁能帮我。 I want to read pdf files from app/src/main/res/Unit1.pdf.My code is 我想从app / src / main / res / Unit1.pdf中读取pdf文件,我的代码是

File file1=new File("app/src/main/res/Unit1.pdf")
Uri path1=Uri.fromFile(file1);
if (file1.exists())
{
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path1, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e){
Toast.makeText(MainActivity.this,"No Application Availabel To View
PDF",Toast.LENGTH_SHORT).show();
}
}

if section not working....plz help..

You need to write the your PDF file present in your app's res folder to sdcard. 您需要将应用程序res文件夹中存在的PDF文件写入sdcard。 So that other apps can read and open it. 以便其他应用程序可以读取并打开它。

As in res folder file is locally available to your app only. 如在res文件夹文件仅在本地可用于您的应用程序。 and you are Throwing an intent for other apps to open that pdf which is not accessable to them. 并且您正打算让其他应用打开无法访问的pdf。 so remove your if check, write your pfd from res to sdcard and pass that file's uri to the intent. 因此删除您的if检查,将pfd从res写入sdcard并将该文件的uri传递给意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM