246

I have a string, 'songchoice'. I want it to become a 'Uri' so I can use with MediaPlayer.create(context, Uri)

How can I convert songchoice to the Uri?

halfer
  • 19,824
  • 17
  • 99
  • 186
James Andrew
  • 7,197
  • 14
  • 46
  • 62

3 Answers3

538
Uri myUri = Uri.parse("http://www.google.com");

Here's the doc http://developer.android.com/reference/android/net/Uri.html#parse%28java.lang.String%29

Peter Ajtai
  • 56,972
  • 13
  • 121
  • 140
10
Uri.parse(STRING);

See doc:

String: an RFC 2396-compliant, encoded URI

Url must be canonicalized before using, like this:

Uri.parse(Uri.decode(STRING));
Anton Savenok
  • 875
  • 8
  • 10
3

The String I had to convert to a URI was a local file path, but without the file:// prefix. So even

 Uri.parse(Uri.decode(STRING));

resulted in FileNotFoundException: No content provider (see also this question). I got a valid URI by first creating a File object i.e.:

Uri myUri = Uri.fromFile(new File(STRING));
Aldinjo
  • 394
  • 1
  • 7
  • 21