Convert file path to URI object emf.common.util.URI - java

I need a solution to convert file path to EMF URI, not a Java URI.
I tried with this:
org.eclipse.emf.common.util.URI ur = org.eclipse.emf.common.util.URI.createURI(URI.createURI(file.getPath()).toString());
...but I get this exception:
java.net.MalformedURLException: unknown protocol: c appears .
Is there another solution?

In EMF, URI classe comes with a lot of static methods to help you create your URI. In your specific case, try URI.createFileURI(...) instead of URI.createURI(...)
URI fileURI = URI.createFileURI(file.getAbsolutePath());
Look here for details about the method: http://download.eclipse.org/modeling/emf/emf/javadoc/2.4.3/org/eclipse/emf/common/util/URI.html#createFileURI(java.lang.String)

Related

Read a file using URL

I am trying to read a file using URL in java.
FileHelper.read(new File(getClass.getResource("TextFile.rtf")))
I am really confused with the below exception
error: overloaded method constructor File with alternatives:
(java.net.URI)java.io.File <and>
(java.lang.String)java.io.File
cannot be applied to (java.net.URL)
Any idea or suggestion how can I resolve this exception.
Thanks !!!
Try converting the URL to its URI equivalent:
FileHelper.read(new File(getClass.getResource("TextFile.rtf").toURI()))
See URL.toURI() for more information.
All you need to do is to get URI from the URL.
URL url = getClass.getResource("TextFile.rtf");
URI uri = url.toURI();
FileHelper.read(new File(uri))

android get URL path

I've got a string:
public://imageifarm/3600.jpg
How can I extract the
imageifarm/3600.jpg
Part out using android?
What I've tried so far:
URL drupalQuestionNodeImageURI = new URL("public://imageifarm/3600.jpg");
Log.d("TAG", drupalQuestionNodeImageURI.getPath());
but it throws this exception:
09-16 17:24:39.992: W/System.err(3763): java.net.MalformedURLException: Unknown protocol: public
How can I solve this?
I know I can use regular expressions but that seems to defeat the purpose of URL(URI) in this case.
You should use android.net.Uri
Uri mUri = Uri.parse(public://imageifarm/3600.jpg);
String extract = mUri.getEncodedSchemeSpecificPart();
Use java.net.URI, not java.net.URL.
If you want have to use URL class (when you image sits on Internet) you have to provide valid URL (that begins from valid URL prefix, like http://, https:// etc). In you case you should use Uri class. Uri object can point on files in your local file system. For example:
Uri.fromFile(new File("public://imageifarm/3600.jpg"));

Not too sure how a URI works regarding absolute paths to files

Simple question: why am I getting new IllegalArgumentException: Path component should be '/' when trying to create a zip filesystem at the following URI:
file:E:/somedirectory/somefile
But this seems to work: file:/somedirectory/somefile
What if I have the same paths on two different drives and I need to access a specific one? Or am I completely missing the point of URIs in the first place?
For paths that use windows volumes use the following format:
file:///e:/somedirectory/somefile
The triple /// results from omitting the URL hostname for local files. Compare: file://sometherhost/e:/somedirectory/somefile, which is valid according to the URI spec, if not actually useful for accessing files on remote volumes.
1. Backslashes are used to point directories and files
2. Try it this way...
`E:\\somedirectory\\somefile`
Maybe its easier to do it with the URI builder. I always use it:
URIBuilder builder = new URIBuilder();
builder.setSchema("file").setHost("anyhost").setPath("/yourpath/");
URI uri;
uri = builder.build();
you can check your URI:
System.out.println(uri.toString());
I hope this will help you!

get specific data from URL in android/java?

I have a url like this
https://example.com%3A443:443/www/36d8e8c94aae45048ba9f92f8e62c7b2/file/content?path=%2Fmnt%2Fsdcard%2Fvideo%2Fsample_mpeg4.mp4&yyy=9a7491a1f5664336b5c6bf431f42852c3749ed54722736ec748746858a710c44
I need to get the path of file as /mnt/sdcard/video from the URL . How do i do that ?
I used URL decoder to remove the spaces but i am confused on getting the path of the file .
Thanks for all your help.
Use Uri.getQueryParameter().
import android.net.Uri;
Uri uri = Uri.parse("https://example.com%3A443:443/www/36d8e8c94aae45048ba9f92f8e62c7b2/file/content?path=%2Fmnt%2Fsdcard%2Fvideo%2Fsample_mpeg4.mp4&yyy=9a7491a1f5664336b5c6bf431f42852c3749ed54722736ec748746858a710c44");
String pathParam = uri.getQueryParameter("path");
You can do it with the Uri class. Construct a Uri object and use Uri.getQueryParameter().

Turning a string into a Uri in Android

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

Categories