Turning a string into a Uri in Android

JavaAndroidUri

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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));

Solution 3 - Java

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));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames AndrewView Question on Stackoverflow
Solution 1 - Javauser132014View Answer on Stackoverflow
Solution 2 - JavaAnton SavenokView Answer on Stackoverflow
Solution 3 - JavaAldinjoView Answer on Stackoverflow