Converting of Uri to String

AndroidStringUri

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

Uri to String

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

String to Uri

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

Solution 2 - Android

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

Solution 3 - Android

using Android KTX library u can parse easily

val uri = myUriString.toUri()

Solution 4 - Android

STRING TO URI.

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

URI TO STRING

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

happy coding :)

Solution 5 - Android

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.

Solution 6 - Android

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

Solution 7 - Android

String to Uri

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

Uri to String

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

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
QuestionNewDroidDevView Question on Stackoverflow
Solution 1 - AndroidRico HarisinView Answer on Stackoverflow
Solution 2 - AndroidSimon K. GergesView Answer on Stackoverflow
Solution 3 - AndroidSwaiXView Answer on Stackoverflow
Solution 4 - AndroidKanagalingamView Answer on Stackoverflow
Solution 5 - AndroidYanbin HuView Answer on Stackoverflow
Solution 6 - AndroidRamkailashView Answer on Stackoverflow
Solution 7 - AndroidDeepakView Answer on Stackoverflow