简体   繁体   中英

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

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. So that other apps can read and open it.

As in res folder file is locally available to your app only. and you are Throwing an intent for other apps to open that pdf which is not accessable to them. so remove your if check, write your pfd from res to sdcard and pass that file's uri to the intent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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