2013-09-27 1 views
0

사용자 지정 listView가 있습니다. 내 어댑터에서 URL을 사용하여 이미지를 설정하려면하지만 얻을 및 IO 예외. 예를 들어이 내가 JSON에서 가져온, URL입니다, 그리고 여기 \/\/socialgummers.com\/demo\/tcibuzz\/wp-content\/uploads\/2013\/07\/S054585.jpgURL을 사용하여 이미지를 설정하지 못했습니다.

이제 브라우저에서 열어 내 어댑터의 코드입니다 : 내가 선을 사용하여 시도

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(list_resourceID, parent, false); 
    final ImageView hotel_pic = (ImageView) rowView 
      .findViewById(R.id.imageView_hotel_1); 
    TextView hotel_name = (TextView) rowView 
      .findViewById(R.id.textview_hotel_1); 
    TextView hotel_name_descrip = (TextView) rowView 
      .findViewById(R.id.textview_hotel_descrip); 
    String hotel = List_Filler.GetbyId(position).getHotel_name(); 
    String descri = List_Filler.GetbyId(position).getShort_description(); 
    hotel_name.setText(hotel); 
    hotel_name_descrip.setText(descri); 
    String temp_url; 
    StringTokenizer tokens = new StringTokenizer(List_Filler.GetbyId(
      position).getHotel_imgs(), "[\""); 
    do { 
     temp_url = (String) tokens.nextElement(); 
    } while (false); 
    URL img = null; 
    try { 
     if (temp_url.length() > 5) 
      img = new URL(temp_url); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 
    try { 
     if (temp_url.length() > 5) { 
      @SuppressWarnings("deprecation") 
      ////Bitmap bitmap = BitmapFactory.decodeStream(img.openConnection() 
       //// .getInputStream()); 
      InputStream is = (InputStream) new URL(temp_url).getContent(); 
      Drawable temp = Drawable.createFromStream(is, "temp"); 
      hotel_pic.setBackgroundDrawable((temp)); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return rowView; 
} 

이를 얻을 수 위의 댓글을 달았습니다 이미지이지만 운이 없다. java.io.IOException : 상대 경로라는 예외가 발생합니다. 내가 이미지를 얻을 어떻게

09-27 16:17:19.770: W/System.err(8906): java.io.IOException: Relative path: \/\/socialgummers.com\/demo\/tcibuzz\/wp-content\/uploads\/2013\/07\/S054585.jpg 
09-27 16:17:19.880: W/System.err(8906):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:235) 
09-27 16:17:19.880: W/System.err(8906):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205) 
09-27 16:17:19.880: W/System.err(8906):  at java.net.URLConnection.getContent(URLConnection.java:197) 
09-27 16:17:19.880: W/System.err(8906):  at java.net.URL.getContent(URL.java:613) 
09-27 16:17:19.880: W/System.err(8906):  at com.tci_buzz.tcibuzz.Hotels_Adapter.getView(Hotels_Adapter.java:70) 
09-27 16:17:19.880: W/System.err(8906):  at android.widget.AbsListView.obtainView(AbsListView.java:1428) 
09-27 16:17:19.880: W/System.err(8906):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1265) 
09-27 16:17:19.880: W/System.err(8906):  at android.widget.ListView.onMeasure(ListView.java:1128) 
09-27 16:17:19.880: W/System.err(8906):  at android.view.View.measure(View.java:8541) 
09-27 16:17:19.880: W/System.err(8906):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566) 
09-27 16:17:19.880: W/System.err(8906):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381) 
09-27 16:17:19.880: W/System.err(8906):  at android.view.View.measure(View.java:8541) 
09-27 16:17:19.880: W/System.err(8906):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3257) 
09-27 16:17:19.880: W/System.err(8906):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 
09-27 16:17:19.880: W/System.err(8906):  at android.view.View.measure(View.java:8541) 
09-27 16:17:19.880: W/System.err(8906):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3257) 
09-27 16:17:19.880: W/System.err(8906):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 
09-27 16:17:19.880: W/System.err(8906):  at android.view.View.measure(View.java:8541) 
09-27 16:17:19.880: W/System.err(8906):  at android.view.ViewRoot.performTraversals(ViewRoot.java:903) 
09-27 16:17:19.890: W/System.err(8906):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1961) 
09-27 16:17:19.890: W/System.err(8906):  at android.os.Handler.dispatchMessage(Handler.java:99) 
09-27 16:17:19.890: W/System.err(8906):  at android.os.Looper.loop(Looper.java:150) 
09-27 16:17:19.890: W/System.err(8906):  at android.app.ActivityThread.main(ActivityThread.java:4293) 
09-27 16:17:19.890: W/System.err(8906):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-27 16:17:19.890: W/System.err(8906):  at java.lang.reflect.Method.invoke(Method.java:507) 
09-27 16:17:19.890: W/System.err(8906):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
09-27 16:17:19.890: W/System.err(8906):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 

: 여기 내 스택 트레이스입니까? 감사.

+0

의 URL HTTP'로 변환, 이유 : // 주소/@SebastianSwierczek 그것을 시도 image.jpg' 버전 – sswierczek

+0

,이 일을 일부러 .. –

+0

어쩌면 그것은 어리석은 일이지만 ... 매니페스트에 인터넷 사용 권한이 있습니까? – sswierczek

답변

1

아마 사용하려고 특별히의 ListView에,이 일을 더 쉽게 만들어 줄게, 제대로 이미지를로드하기 위해 외부 라이브러리를 사용하여 고려하십시오 (작동한다)

 Bitmap temp = null; 
     try { 
      if (temp_url.length() > 5) {      
        temp = BitmapFactory.decodeStream((InputStream)new URL(temp_url).getContent()); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     hotel_pic.setImageBitmap(temp); 

추신 메인 쓰레드 내에서 실행해서는 안된다. AsyncTask로 옮겨야한다.

http://domain 형식의 올바른 URL을 temp_urlString 개체 안에 저장해야합니다.

편집 : (전체 코드는)

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
LayoutInflater inflater = (LayoutInflater) context 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View rowView = inflater.inflate(list_resourceID, parent, false); 
final ImageView hotel_pic = (ImageView) rowView 
     .findViewById(R.id.imageView_hotel_1); 
TextView hotel_name = (TextView) rowView 
     .findViewById(R.id.textview_hotel_1); 
TextView hotel_name_descrip = (TextView) rowView 
     .findViewById(R.id.textview_hotel_descrip); 
String hotel = List_Filler.GetbyId(position).getHotel_name(); 
String descri = List_Filler.GetbyId(position).getShort_description(); 
hotel_name.setText(hotel); 
hotel_name_descrip.setText(descri); 
String temp_url; 
StringTokenizer tokens = new StringTokenizer(List_Filler.GetbyId(
     position).getHotel_imgs(), "[\""); 
do { 
    temp_url = (String) tokens.nextElement(); 
} while (false); 


temp_url = "http://" + temp_url.replace("\",""); 
Bitmap temp = null; 
try { 
    if (temp_url.length() > 5) {     
     temp = BitmapFactory.decodeStream((InputStream)new URL(temp_url).getContent()); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
hotel_pic.setImageBitmap(temp); 

return rowView; 
} 
+0

나는 이것을 시도했지만 작동하지 않았다. –

+0

나는 그것을 시도하고 그것은 확실히 작동합니다. 'temp_url = "http : //"+ temp_url.replace ("\", "");' – sswierczek

+0

정말 고마워요. –

0
InputStream is = (InputStream) new URL("http:"+temp_url).getContent(); 
+0

응답 해 주셔서 감사합니다.하지만이 시도했지만 여전히 동일한 오류가 발생합니다. –