2

I want to check if a pdf-file (this one: http://www.augustinianum.eu/roosterwijzigingen/14062012.pdf) I tried this:

File fileTest = new File("http://www.augustinianum.eu/roosterwijzigingen/14062012.pdf");
 if(fileTest.exists()){
     //DO STUFF
 }

But it doesn't execute the if block. How is that possible? The PDF does exist.

It's probably an easy question, but I don't know how to fix it.

Thank You!

Xander
  • 5,487
  • 14
  • 49
  • 77

2 Answers2

2

Actually, the URL of file is for the server you can't access File like this,

For this you have to first download this file on android device, then using File class of Android you can check its existence.

Example:

Suppose you downloaded file on sdcard from this URL using HttpPost or URLConnection,

then

File fileTest = new File("/mnt/sdcard/14062012.pdf"); //Virtually path of your pdf file after download
 if(fileTest.exists()){
     //DO STUFF
 }

Now your if block will executed..

user370305
  • 108,599
  • 23
  • 164
  • 151
  • Hmm, isn't there another way to check wheter that pdf exists on the web or not? – Xander Aug 12 '12 at 18:20
  • Yes Look at this SO question [Java check if file exists on remote server using its url](http://stackoverflow.com/questions/4596447/java-check-if-file-exists-on-remote-server-using-its-url) – user370305 Aug 12 '12 at 18:24
  • And to download file from server http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device – user370305 Aug 12 '12 at 18:28
  • But what I don't understand is this: developer.android.com says there's a constructor method which is `File(URI uri)`, but when I create a URI this way `URI uriTest = new URI("http://www.augustinianum.eu/roosterwijzigingen/14062012.pdf");` and make a file this way `File fileTest = new File(uriTest);` I'm getting a force close. Why is that? – Xander Aug 12 '12 at 18:47
  • Its for `Uri` not for `URL`. Actually, the constructor `File(Uri)` is for specified resource uri respect to other applications. Look at http://developer.android.com/reference/java/io/File.html#File(java.net.URI) – user370305 Aug 12 '12 at 19:19
  • And if I'd make my code download the file. Will the user see the download icon in the statusbar? – Xander Aug 13 '12 at 07:58
  • No, If you implement download code within your application user can not seen Download Icon on Statusbar. – user370305 Aug 13 '12 at 09:01
  • And is it possible to delete that downloaded pdf-file in my code? – Xander Aug 13 '12 at 09:27
  • And one more question: Will the app force close when I command it to download a file which is not available? (BTW I really appreciate your help and patience, ty) – Xander Aug 13 '12 at 12:53
1

I don't think you can directly access file like this using File class.

You may need to consider using HTTPURLConnection API for this purpose.

kosa
  • 65,990
  • 13
  • 130
  • 167