我想从我的资产文件夹中读取pdf文件,但我不知道如何获得pdf文件的路径。 我右键点击PDF文件,然后选择“复制路径”并粘贴如何从资产文件夹创建文件对象?
这里是我的代码:
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()成功。我正在使用Mac版的Android studio。谢谢
http://stackoverflow.com/questions/9544737/read-file-from-assets的可能重复尝试 –
我想创建'文件'在上面一个创建InputStream – Zulqarnain
http://stackoverflow.com/questions/17085574/read-a-pdf-file-from-assets-folder检查此,这是类似于你想要实现 –