2012-06-08 3 views
0

내 안드로이드 응용 프로그램에 이미지를 표시해야합니다. 문제는 온라인 어딘가에서이 이미지를 얻고 싶다는 것입니다. URL은 같은 :온라인에서 파일 이름에 공백이있는 안드로이드 이미지를 표시

http://www.mydomain.com/hello 세계 당신이 일부 공간을 포함하는 이미지 이름을 참조로

을 image.png를. 내 코드를 실행할 때마다 FileNotFound 예외가 표시됩니다. 아무 일도 일어나지 않습니다. 다음

내가 공간을 대체하기 위해 사용하는 내 코드

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(); 
} 

이다 "+"하지만 아무 반응이 없습니다.

+0

브라우저에서 URL을 직접 누르는 중 이미지가 표시됩니까? – SRam

+0

다음과 같이 URL 인코딩을 시도하십시오 :'String encodedurl = URLEncoder.encode (url, "UTF-8");' –

+0

@zeeshan pls는 도움이되는 사람에게 답변을 수락합니다 – SRam

답변

2

이 작동합니다.

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

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

당신은 ...

하여 사용하는 URL의 공간을 대체 ... 이미지를 얻기 위해 http://en.wikipedia.org/wiki/Percent-encoding#Character_data

0

는이 같은 인코딩에게 경로를 URL로 필요

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

저는 내 라인을 다음과 같이 변경합니다 : 'imagePath = "http://www.mydomain.com/"+ URLEncoder.encode ("hello world image.png"), "utf-8"), \t \t IT 부서에서 여전히 파일을 찾을 수 없습니다. – zeeshan

3

plese 사용 올바른 URL을 알고의 definetly U 도움이 될 코드 아래 사용해야합니다

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

그리고 지금 ...

  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); 
+0

제 경우에는 작동하지 않습니다. 예외는 없지만 표시 할 이미지가 없습니다. – zeeshan

+0

코드가 완벽하게 작동합니다. String Replace를 사용하여 ""을 % 20으로 바꿉니다. – zeeshan

+0

듣기 좋은 ... – SRam

0

http get 요청을 사용할 수도 있습니다. 가장 간단한 방법은 URL 인코딩 된 형식으로 문자열의 모든 문자를 자동으로 바꾸는 URLEncoder를 호출하는 것입니다.

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 
관련 문제