我正试图从我的资产文件夹中读取一个pdf文件,但我不知道如何获取pdf文件的路径。我右键单击pdf文件并选择“复制路径”,然后粘贴它。
以下是我的代码的另一个截图:
这是我的代码:
![enter image description here](https://istack.dev59.com/geGSo.webp)
![enter image description here](https://istack.dev59.com/ar8pk.webp)
File file = new File("/Users/zulqarnainmustafa/Desktop/ReadPdfFile/app/src/main/assets/Introduction.pdf");
if (file.exists()){
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 {
this.startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(this, "No application available to view PDF", Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(this, "File path not found", Toast.LENGTH_LONG).show();
}
我总是得到文件未找到的错误,请帮我创建File对象或让我知道如何获取文件的确切路径。我也尝试了file:///android_asset/Introduction.pdf
,但没有成功。我还尝试了Image.png,但从来没有得到过file.exists()的成功。我正在使用Android Studio的Mac版本。谢谢。