0

カスタムリストビューがあります。私のアダプターでは、URL を使用して画像を設定したいのですが、IO 例外が発生します。たとえば、これは JSON から取得した URL で、ブラウザで開きます \/\/socialgummers.com\/demo\/tcibuzz\/wp-content\/uploads\/2013\/07\/S054585.jpg

ここに私のアダプターのコードがあります:

@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: Relative path という例外が発生します。ここに私のスタックトレースがあります:

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)

どうすれば画像を取得できますか? ありがとう。

4

3 に答える 3

1

たぶん使用してみてください:(動作するはずです)

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

PSメインスレッド内で行うべきではなく、AsyncTaskに移動してください。

オブジェクトhttp://domain内の形式で適切な URL を提供することを忘れないでください。temp_url String

編集:(完全なコード)

@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;
}
于 2013-09-27T13:33:23.360 に答える
0
InputStream is = (InputStream) new URL("http:"+temp_url).getContent();
于 2013-09-27T11:41:01.680 に答える