我正在尝试读取文件 . 我不介意的位置,所以我尝试了各种位置和方法来做到这一点:
File file = new File("DFirstPdf.pdf");
File file = new File("C:/DFirstPdf.pdf");
File file = new File("C:\\DFirstPdf.pdf");
但我总是得到那个文件不存在 . 我正在使用Eclipse和模拟器,我没有更多的想法 .
我想要做的是: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);
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
if (activities.size() > 0)
{
startActivity(intent);
}
else
{
// Do something else here. Maybe pop up a Dialog or Toast
}
2 回答
您必须使用类加载器来获取资源
. PackageManager.class.getClassLoader()的getResource( “DFirstPdf.pdf”);
模拟器有自己的文件系统 . 它不会在您的计算机上看到文件 . 它有一个UNIX风格的路径,而不是一个Windows风格的路径 .