我想從我的資產文件夾中讀取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檢查此,這是類似於你想要實現 –