Display image in android with spaces in filename from online

北城余情 提交于 2021-01-28 23:17:44

问题


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:

http://www.mydomain.com/hello world image.png

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.

Following is my code

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.


回答1:


This will work.

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

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

You must know http://en.wikipedia.org/wiki/Percent-encoding#Character_data




回答2:


plese use correct Url for getting image and use below code that would definetly help u...

replace space of URL using...

  imagePath=imagePath.replaceAll(" ", "%20");

and now...

         HttpGet httpRequest = new HttpGet(new URL(params[0]).toURI());
         HttpClient httpClient = new DefaultHttpClient();
         HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
         HttpEntity entity = response.getEntity();
         BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
             InputStream is = bufHttpEntity.getContent();
        //image_value = new URL("image Url is here");
            bm = BitmapFactory.decodeStream(new FlushedInputStream(is));
    //imageLoader is object of iamge view
           imageLoader.setImageBitmap(bm);



回答3:


You need to url encode the path, like this:

String imagePathCon = URLEncoder.encode("hello world image.png", "UTF-8");



回答4:


You can also use an http get request: The simplest way is to simply call URLEncoder which will automatically replace all charaters in your string to the url encoded format.

HttpParams httpParameters = new BasicHttpParams(); 
HttpConnectionParams.setConnectionTimeout(httpParameters, 30000); 
HttpConnectionParams.setSoTimeout(httpParameters, 30000);
HttpClient httpclient = new DefaultHttpClient(httpParameters); 
HttpGet httpGet = new HttpGet(URLEncoder.encode(url, "UTF-8"));
HttpResponse httpResponse = httpclient.execute(httpGet); 
in = httpResponse.getEntity().getContent(); 
//Bitmap bmp = BitmapFactory.decodeStream(instream); 
//create a bitmap or an image from the input stream


来源:https://stackoverflow.com/questions/10944320/display-image-in-android-with-spaces-in-filename-from-online

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!