开发者

Turning a string into a Uri in Android

开发者 https://www.devze.com 2022-12-27 22:02 出处:网络
I have a string, \'songchoice\'. I want it to become a \'Uri\' so I can use with MediaPlayer.crea开发者_Python百科te(context, Uri)

I have a string, 'songchoice'. I want it to become a 'Uri' so I can use with MediaPlayer.crea开发者_Python百科te(context, Uri)

How can I convert songchoice to the Uri?


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消