无法使用URL设置图像

时间:2013-09-27 11:26:57

标签: java android listview ioexception

我有一个自定义listView。在我的适配器中,我想使用URL设置图像,但我得到和IO异常。例如,这是我从JSON获得的网址,它在我的浏览器中打开\/\/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:相对路径。 这是我的stacktrace:

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)

我如何获得图像?感谢。

3 个答案:

答案 0 :(得分: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);

P.S。你不应该在主线程中执行它,将它移动到AsyncTask。

请务必在http://domain 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;
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

即使它有效,我担心它不会按你想要的方式工作。由于ListView的工作原理,当您下载图像时,它可能会被放置在错误的行中。

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

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

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