126

Is it possible to convert an Uri to String and vice versa? Because I want to get the the Uri converted into String to pass into another activity via intent.putextra() and if it's not possible can anyone suggest me a way how to pass selected Uri into another activity?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
NewDroidDev
  • 1,656
  • 5
  • 19
  • 33
  • 4
    lol why is this question marked -2 ? `Uri.parse()` is not intuitive, and especially not when the reverse is `uri.toString();` (i would have thought it would be `Uri.stringify()` or something. definitely upvoting this question. – David T. Nov 05 '13 at 02:00

7 Answers7

322

Uri to String

Uri uri;
String stringUri;
stringUri = uri.toString();

String to Uri

Uri uri;
String stringUri;
uri = Uri.parse(stringUri);
Rico Harisin
  • 3,319
  • 1
  • 14
  • 7
11

Uri is serializable, so you can save strings and convert it back when loading

when saving

String str = myUri.toString();

and when loading

Uri myUri = Uri.parse(str);
merlin2011
  • 71,677
  • 44
  • 195
  • 329
Simon K. Gerges
  • 3,097
  • 36
  • 34
4

using Android KTX library u can parse easily

val uri = myUriString.toUri()
SwaiX
  • 79
  • 1
  • 5
3

STRING TO URI.

Uri uri=Uri.parse("YourString");

URI TO STRING

Uri uri;
String andro=uri.toString();

happy coding :)

Kanagalingam
  • 2,096
  • 5
  • 23
  • 40
3

If you want to pass a Uri to another activity, try the method intent.setData(Uri uri) https://developer.android.com/reference/android/content/Intent.html#setData(android.net.Uri)

In another activity, via intent.getData() to obtain the Uri.

Yanbin Hu
  • 537
  • 6
  • 18
0

You can use .toString method to convert Uri to String in java

Uri uri = Uri.parse("Http://www.google.com");

String url = uri.toString();

This method convert Uri to String easily

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Ramkailash
  • 1,852
  • 1
  • 23
  • 19
0

String to Uri

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

Uri to String

Uri uri;
String stringUri = uri.toString();
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Deepak
  • 131
  • 2
  • 12