本文介绍了在Android中显示图片,并在线显示文件名中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的android应用程序中显示图像.问题是我想从网上某个地方获取此图像.网址类似:

I need to display an image in my android application. The problem is I want to get this image from somewhere online. The URL is like:

如您所见,图像名称中包含一些空格.每次执行代码时,都会显示FileNotFound异常.并没有任何反应.

As you see the image name containing some spaces in it. Every time I execute my code this will show me exception of FileNotFound. and nothing happens.

以下是我的代码

String imagePathCon = "hello world image.png";
String imagePath = "http://www.mydomain.com/" + imagePathCon;
try {
  URL url;
  url = new URL(imagePath);

  // url = new URL("http://www.azuma-kinba.com/wp-content/uploads/2012/05/Android-Make-Google-Loss-in-2010.png");

  InputStream content = (InputStream)url.getContent();
  Drawable d = Drawable.createFromStream(content , "src");
  im.setImageDrawable(d);

 } catch (MalformedURLException e) {
            e.printStackTrace();
 } catch (IOException e) {
            e.printStackTrace();
 }

我曾经用"+"代替空格,但是什么也没发生.

I use to replace space with "+" but nothing happens.

推荐答案

这将起作用.

String imagePathCon = "hello world image.png";
imagePathCon=imagePathCon.replaceAll(" ", "%20");

String imagePath = "http://www.mydomain.com/" + imagePathCon;

您必须了解 http://en.wikipedia.org/wiki/Percent-encoding#Character_data

这篇关于在Android中显示图片,并在线显示文件名中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:38