0

Does anyone has an idea why this cast is not good:

URI myUri = URI.create("http://storage.googleapis.com/autoplay_audio/titanium.mp3");
File f = new File(myUri);
chiwangc
  • 3,566
  • 16
  • 26
  • 32
davidmoshko
  • 223
  • 1
  • 4
  • 8
  • What error are you getting? – phuzi May 18 '15 at 15:12
  • FYI: What you are doing is not casting. You are creating a `URI` instance and then passing that to the constructor of `File.` Casting would look something like `File f = (File) myUri;` which will not work. – MadConan May 18 '15 at 15:17

2 Answers2

0

You cannot use anything other than a file uri (eg file://).

The constructor File(URI) has the following condition check:

String scheme = uri.getScheme();
if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
        throw new IllegalArgumentException("URI scheme is not \"file\"");

If you intend to connect to an http uri, you'll need to use some other mechanism, such as URL.

MadConan
  • 3,749
  • 1
  • 16
  • 27
0

You can use File only for files. For your example to work, File would need to be aware of the HTTP protocol. You should use Apache HttpClient or some other framework depending on your needs and environment.

Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110
  • The reason I do all this is because I want to get the audio's duration and I found a function that returns the duration of an audio file in Java File format. How do you suggest me to do it best? – davidmoshko May 18 '15 at 15:36
  • You can download the file with HttpClient for example, as described [here](http://stackoverflow.com/questions/10960409/how-do-i-save-a-file-downloaded-with-httpclient-into-a-specific-folder). Then you can pass that File to the library which requires it. – Dragan Bozanovic May 18 '15 at 15:42
  • The file is located in Google App Engine server. Isn't there a way to get the file duration without downloading the file? – davidmoshko May 18 '15 at 16:47