我有一个关于从网站加载图像的问题.我使用的代码是:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Bitmap bit=null;
try {
bit = BitmapFactory.decodeStream((InputStream)new URL("http://www.mac-wallpapers.com/bulkupload/wallpapers/Apple%20Wallpapers/apple-black-logo-wallpaper.jpg").getContent());
} catch (Exception e) {}
Bitmap sc = Bitmap.createScaledBitmap(bit,width,height,true);
canvas.drawBitmap(sc,0,0,null);
Run Code Online (Sandbox Code Playgroud)
但它总是返回一个空指针异常,程序崩溃了.该URL有效,似乎适用于其他所有人.我正在使用2.3.1.
我正在尝试创建一个自定义列表适配器,其中包含需要从Internet下载的每个项目的图像.当我第一次进入活动时 - 应用程序冻结一段时间,直到下载图像,然后加载活动列表.
我可以看出,在活动加载之前正在下载图像.如何在活动加载后下载图像.我想我需要使用Async Task.但由于我在自定义阵列适配器中加载图像,因此不确定如何操作.
这是我的自定义适配器getView():
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list_item_default, null);
}
Item p = items.get(position);
if (p != null) {
TextView list_title = (TextView) v.findViewById(R.id.list_title);
TextView list_description = (TextView) v
.findViewById(R.id.list_description);
TextView list_timestamp = (TextView) v
.findViewById(R.id.list_timestamp);
ImageView list_image = (ImageView) v.findViewById(R.id.list_image);
if (list_title != null) {
list_title.setText(p.getItemTitle());
}
if (list_description != null) {
list_description.setText(p.getItemDescription()); …Run Code Online (Sandbox Code Playgroud)