0

I am trying to read a file. I do not mind the location, so I have tried various locations and ways to do this:

File file = new File("DFirstPdf.pdf");
File file = new File("C:/DFirstPdf.pdf");
File file = new File("C:\\DFirstPdf.pdf");

But I always get that file does not exist. I am working whit Eclipse and a simulator and I do not have more ideas.

What I want to do is: 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
}
user2173859
  • 57
  • 10

2 Answers2

1

You have to use the classloader to get the resource

PackageManager.class.getClassLoader().getResource("DFirstPdf.pdf");

Jirawat Uttayaya
  • 1,039
  • 9
  • 11
  • I have done the following code File file = new File((PackageManager.class.getClassLoader().getResource("D://DFirstPdf.pdf").toURI())); and File file = new File((PackageManager.class.getClassLoader().getResource("DFirstPdf.pdf").toURI())); but I get a Null Pinter Exception, Where show I have the file stored? Thank you – user2173859 Mar 18 '13 at 08:51
0

THe simulator has its own filesystem. It won't see files on your machine. ANd it has a UNIX style path, not a windows style one.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • So, How can I do to get a file.PDF, where Can I store it, to loading it after? any other suggestion? – user2173859 Mar 18 '13 at 08:31
  • Load it in your apk, or if you don't want all your users to have that file (its a test file, for example), put it on your emulator with adb push. Your app can save it to the emulator's simulated internal or external storage. – Gabe Sechan Mar 18 '13 at 14:12
  • That is a good idea, but, sorry, how can i do that, Its a test file I do not mind all users have it. – user2173859 Mar 19 '13 at 08:46