简体   繁体   中英

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

使用java.net.URI,而不是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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM