不良URL编码图像 [英] Bad URL encoding for images
问题描述
在我的应用程序,有一个从URL显示图像的ImageView的。
我用这个方法下载图片:
位图位= NULL;
URL =图片网址新的URL(网址);
HttpURLConnection的康恩=(HttpURLConnection类)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(真);
InputStream为= conn.getInputStream();
OutputStream的OS =新的FileOutputStream(F);
Utils.CopyStream(是,OS);
os.close();
位=去codeFILE(F);
返回位图;
这仅适用于有数字或英文字母,并且不与任何其他工作字符(如空格)网址:
好网址: http://site.com/images/image。 PNG
坏网址: http://site.com/images/image 1.png
我试图恰克URL编码(URLEn coder.en code),但它改变了整个URL(incliding斜杠,等...)。
我需要的编码后,以取代一些字符?或者有更好的办法吗?
感谢:)
在Android的< URL编码/一>
您不带code。整个URL,只是它的一部分是来自
不可靠的来源。 -yanchenko
块引用>查询字符串= URLEn coder.en code(苹果橘子,UTF-8);
字符串URL =http://stackoverflow.com/search?q=+查询;
这是好的,如果你只是处理一个URL的特定部分和
你知道如何构建或重建的URL。一个更一般的
的做法,可以处理任何URL字符串,请参阅我的回答如下。 - 克雷格
乙
块引用>字符串urlStr =http://abc.dev.domain.com/0007AC/ads/800x480 15秒h.264.mp4
网址URL =新的URL(urlStr);
的URI的uri =新的URI(url.getProtocol(),url.getUserInfo(),url.getHost(),url.getPort(),url.getPath(),url.getQuery(),url.getRef());
URL = URI.toURL()根据;In my app, there is an ImageView that display an image from a URL.
I download the image using this method:
Bitmap bitmap=null; URL imageUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setInstanceFollowRedirects(true); InputStream is=conn.getInputStream(); OutputStream os = new FileOutputStream(f); Utils.CopyStream(is, os); os.close(); bitmap = decodeFile(f); return bitmap;
This works only with URLs that has numbers or english letters and doesn't work with any other chars (like spaces):
Good URL: http://site.com/images/image.png
Bad URL: http://site.com/images/image 1.png
I tried to chage the URL encoding (URLEncoder.encode), but it changes the whole URL (incliding slashes, ect...).
Do I need to replace some chars after the encoding? or maybe there is a better way?
Thanks :)
解决方案You don't encode the entire URL, only parts of it that come from "unreliable sources". -yanchenko
String query = URLEncoder.encode("apples oranges", "utf-8"); String url = "http://stackoverflow.com/search?q=" + query;
This is fine if you are just dealing with a specific part of a URL and you know how to construct or reconstruct the URL. For a more general approach which can handle any url string, see my answer below. – Craig B
String urlStr = "http://abc.dev.domain.com/0007AC/ads/800x480 15sec h.264.mp4"; URL url = new URL(urlStr); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); url = uri.toURL();
这篇关于不良URL编码图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!