我正在从 URL 下载图像,但 URL 中有空格,因此它会跳过那些 URL。我已经阅读了几篇文章,其中提到用 %20 或 + 替换空格,这两种方法都不起作用。那么现在有什么替代方案。
Log.i("CountryFlagThumb", VArray.get(2).replaceAll(" ", "%20"));
http://id8lab.net/WorldNewsApp/flags/United Arab Emirates.png
谢谢
我正在从 URL 下载图像,但 URL 中有空格,因此它会跳过那些 URL。我已经阅读了几篇文章,其中提到用 %20 或 + 替换空格,这两种方法都不起作用。那么现在有什么替代方案。
Log.i("CountryFlagThumb", VArray.get(2).replaceAll(" ", "%20"));
http://id8lab.net/WorldNewsApp/flags/United Arab Emirates.png
谢谢
您不对整个 URL 进行编码,仅对来自“不可靠来源”的部分进行编码。
String data = URLEncoder.encode("United Arab Emirates.png", "utf-8");
String url = "http://id8lab.net/WorldNewsApp/flags/" + data;
您只需像这样对您的网址进行编码
字符串 url =Uri.encode("http://id8lab.net/WorldNewsApp/flags/United Arab Emirates.png")
希望这会有所帮助。
要从 url 下载图像,请使用以下命令,
HttpGet httpRequest = new HttpGet(URI.create(path) );
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
Bitmap bmp = BitmapFactory.decodeStream(bufHttpEntity.getContent());
httpRequest.abort();