Android- how can I convert android.net.Uri object to java.net.URI object?
You could use the toString
method of the android Uri
in combination of the String
based constructor of the Java URI
.
android.net.Uri auri = new android.net.Uri(what ever);
java.net.URI juri = new java.net.URI(auri.toString());
Android URI |
Java URI
Convert from URI to Uri and the other way around
Both snippets worked for me.
Process-1
Uri newUri = Uri.parse(androidUri.toString());
Process-2
URI androidUri;
Uri newUri = new Uri.Builder().scheme(androidUri.getScheme())
.encodedAuthority(androidUri.getRawAuthority())
.encodedPath(androidUri.getRawPath())
.query(androidUri.getRawQuery())
.fragment(androidUri.getRawFragment())
.build();
convert android.net.Uri to java.net.uri
The problem you have is that a File must be a file on disk. That means the URI must start with file:/// Your URI doesn't. Either your URI is malformed, or it isn't to a local file. Either of those would need to be fixed.
How to convert android.net.Uri to java.net.URL?
If the scheme of the Uri
is http
or https
, new URL(uri.toString())
should work.
If the scheme of the Uri
is file
, that may still also work, though I get nervous.
If the scheme of the Uri
is ftp
or jar
, that may still also work, though we will wonder why you have a Uri
with those schemes.
If the scheme of the Uri
is anything else — such as content
— this will not work, and you need to find a better library.
Convert android.net.Uri to java.net.URL
To show image from drawables no need to use java.net.URI
. do it as:
Uri uriImage = Uri.parse("drawable://" + R.drawable.icon_bar);
imgLoader.DisplayImage(String.valueOf(uriImage), loader, iconBar_View);
How to convert Android Uri to Java URI
Got it figured out, I was able to get the real filepath of the Uri and then create the File:
Uri uri = Uri.parse("content://media/external/images/media/47");
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cur = managedQuery(uri, projection, null, null, null);
cur.moveToFirst();
String path = cur.getString(cur.getColumnIndex(MediaStore.Images.Media.DATA));
mediaFile = new File(path);
Android: construct a File from an android.net.URI
How can I convert a URI like this to a
File object?
You can't, in a reliable, SDK-compliant fashion.
"If the table entry is a content: URI, you should never try to open and read the file directly (for one thing, permissions problems can make this fail). Instead, you should call ContentResolver.openInputStream() to get an InputStream object that you can use to read the data. "
(from http://developer.android.com/guide/topics/providers/content-providers.html)
You will need to modify the other code to accept and utilize an InputStream
. Or, if the other code is supposed to be modifying the data, it will need to use an OutputStream
, a topic also covered in the page linked to above.
Can't convert object of type java.lang.String to type android.net.Uri getting value to object
There is no requirement to pass a class instance to Realtime Database using getValue(class)
to get the data out of a snapshot. If you require some custom mapping to get data into an object, you can simply access the data in the snapshot as using data.getValue() with no parameters. If the child you queried has other children, it will return a Map<String, Object>
. The keys of the map will be the names of the children, and the values will be the data. You can then convert any strings to an Uri if you want.
Related Topics
Getting Certificate Details from an APK
Android Webview, Scaling Image to Fit the Screen
App Always Starts Fresh from Root Activity Instead of Resuming Background State (Known Bug)
How to Capture Image from Custom Cameraview in Android
Using Notifyitemremoved or Notifydatasetchanged with Recyclerview in Android
Android: How to Create a Motionevent
How to Use Android Canvas to Draw a Rectangle with Only Topleft and Topright Corners Round
Rawquery(Query, Selectionargs)
How to Request Root Access in Android
How to Fix "Failed to Sync Vcpu Reg" Error
Android and Setting Alpha for (Image) View Alpha
How to Use Isineditmode() to See Layout with Custom View in the Editor
Android Mapview -Setting Zoom Automatically Until All Itemizedoverlay's Are Visible
Insertion of Thousands of Contact Entries Using Applybatch Is Slow
How to Measure Textview Height Based on Device Width and Font Size