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:相对路径。这是我的堆栈跟踪:

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

即使它起作用了,恐怕它也不会像你想要的那样起作用。由于 ListView 的工作方式,当您下载图像时,它可能被放置在错误的行中。

考虑使用外部库来正确加载图像,它会让事情变得更容易,特别是在 ListViews

http://square.github.io/picasso/

https://github.com/nostra13/Android-Universal-Image-Loader

于 2013-09-27T11:44:13.677 回答
0
InputStream is = (InputStream) new URL("http:"+temp_url).getContent();
于 2013-09-27T11:41:01.680 回答