下载图像从URL与空间在Android的路径 [英] Downloading image from URL with spaces in the path in Android
本文介绍了下载图像从URL与空间在Android的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从路径下载图像。 这是我的code:
I want to download image from the path. This is my code:
String urlString = "http://www.hospimedica.com/images/stories/articles/article_images/_CC/20110328 - DJB146.gif";
url = new URL(urlString.replaceAll(" ", "%20"));
bm = BitmapFactory.decodeStream(url.openConnection().getInputStream());
但它返回我的BM空。任何想法如何使这个code的工作?
But it returns me null in bm. Any ideas how to make this code to work?
推荐答案
的空间都没有问题......我想它与你的网址,它的工作
The spaces are not the problem ... I tried it with your url and it's working
try {
String urlString = "http://www.hospimedica.com/images/stories/articles/article_images/_CC/20110328 - DJB146.gif";
URL url = new URL(urlString.replaceAll(" ", "%20"));
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-agent", "Mozilla/4.0");
connection.connect();
InputStream input = connection.getInputStream();
Log.d("#####", "result: " + BitmapFactory.decodeStream(input));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
最重要的线是 connection.setRequestProperty(用户代理,Mozilla的/ 4.0);
我不知道是什么原因,解决它,但它之前显然已经在这里工作。
The important line is connection.setRequestProperty("User-agent", "Mozilla/4.0");
I don't know why that solves it, but it has obviously worked before here.
这篇关于下载图像从URL与空间在Android的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文