我有一个自定义列表视图。在我的适配器中,我想使用 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)
我如何获得图像?谢谢。