android get URL path - java

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

Related

android.net.Uri ambiguous authority or path

Given the following Android Java code:
import android.net.Uri;
String humanEnteredString = "google.com";
Uri uri = Uri.parse(humanEnteredString);
Why is uri.getPath() == "google.com", and not uri.getAuthority()?
How to force Uri.parse to see "google.com" as the authority?
Is that what classes like this are for?
https://github.com/android/platform_frameworks_base/blob/master/core/java/android/net/WebAddress.java
Why is this WebAddress class not accessible in the Android SDK?
Is there any pre-built class that can generate a valid <scheme>://<authority><path> browsable Uri from a human entered string that may be missing the scheme?
Obviously, I can test for and prefix "http[s]://" as necessary/appropriate, but shouldn't there already be a bulletproof class that does this already?
Thanks,
Pv
Why is uri.getPath() == "google.com", and not uri.getAuthority()?
Because "google.com" is not a valid Uri.
How to force Uri.parse to see "google.com" as the authority?
Use a valid Uri: one with a scheme, such as https://google.com.
Is there any pre-built class that can generate a valid :// browsable Uri from a human entered string that may be missing the scheme?
Not in the Android SDK, at least that I can recall.

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

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)

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

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!

java.net.URI chokes on special characters in host part

I have a URI string like the following:
http://www.christlichepartei%F6sterreichs.at/steiermark/
I'm creating a java.lang.URI instance with this string and it succeeds but when I want to retrieve the host it returns null. Opera and Firefox also choke on this URL if I enter it exactly as shown above. But shouldn't the URI class throw a URISyntaxException if it is invalid? How can I detect that the URI is illegal then?
It also behaves the same when I decode the string using URLDecoder which yields
http://www.christlicheparteiösterreichs.at/steiermark/
Now this is accepted by Opera and Firefox but java.net.URI still doesn't like it. How can I deal with such a URL?
thanks
Java 6 has IDN class to work with internationalized domain names. So, the following produces URI with encoded hostname:
URI u = new URI("http://" + IDN.toASCII("www.christlicheparteiösterreichs.at") + "/steiermark/");
The correct way to encode non-ASCII characters in hostnames is known as "Punycode".
URI throws an URISyntaxException, when you choose the appropriate constructor:
URI someUri=new URI("http","www.christlicheparteiösterreichs.at","/steiermark",null);
java.net.URISyntaxException: Illegal character in hostname at index 28: http://www.christlicheparteiösterreichs.at/steiermark
You can use IDN for this to fix:
URI someUri=new URI("http",IDN.toASCII("www.christlicheparteiösterreichs.at"),"/steiermark",null);
System.out.println(someUri);
System.out.println("host: "+someUri.getHost()));
Output:
http://www.xn--christlicheparteisterreichs-5yc.at/steiermark
host: www.xn--christlicheparteisterreichs-5yc.at
UPDATE regarding the chicken-egg-problem:
You can let URL do the job:
public static URI createSafeURI(final URL someURL) throws URISyntaxException
{
return new URI(someURL.getProtocol(),someURL.getUserInfo(),IDN.toASCII(someURL.getHost()),someURL.getPort(),someURL.getPath(),someURL.getQuery(),someURL.getRef());
}
URI raoul=createSafeURI(new URL("http://www.christlicheparteiösterreichs.at/steiermark/readme.html#important"));
This is just a quick-shot, it is not checked all issues concerning converting an URL to an URI. Use it as a starting point.

Categories