【发布时间】:2013-09-11 13:03:03
【问题描述】:
我正在尝试从网络下载图像并将其显示在图像视图中。 问题是屏幕上没有显示任何内容。 我正在使用下面的代码。 谢谢!
package com.example.citiestoremember;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class DisplayImage extends Activity {
ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_city);
Intent theIntent = getIntent();
String urlImage = theIntent.getStringExtra("urlImage");
try {
img = (ImageView) findViewById(R.id.urlImage);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(urlImage).getContent());
img.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
我建议你使用 ImageLoader 而不是你所用的 github.com/nostra13/Android-Universal-Image-Loader