来自URL的图像位图

时间:2014-08-14 12:04:01

标签: java android inputstream

我正在尝试从字符串数组中的URL获取图像,而我的应用程序正在崩溃。它在第110行崩溃:InputStream input = connection.getInputStream();我做错了什么?

这是我的班级:

package kyfb.android.kyfb.com.kyfb;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.Image;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by KFB on 8/12/14.
 */
class ActionAlertsAdapter extends BaseAdapter {
    private LayoutInflater inflater;

    public ActionAlertsAdapter(Context context) {

        inflater = LayoutInflater.from(context);
    }

    public int getCount() {
        // TODO Auto-generated method stub

        return ActionAlertsFragment.title.length;
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;

        if(convertView == null){
            convertView = inflater.inflate(R.layout.item_feed, null);
            holder = new ViewHolder();
            holder.lytItemFeed = (RelativeLayout) convertView.findViewById(R.id.lytItemFeed);
            holder.txtTitle= (TextView) convertView.findViewById(R.id.txtTitle);
            holder.txtPubDate = (TextView) convertView.findViewById(R.id.txtPubDate);
            holder.thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        if((position%2)!=0){
            holder.lytItemFeed.setBackgroundColor(Color.TRANSPARENT);
        }else{
            holder.lytItemFeed.setBackgroundColor(Color.TRANSPARENT);
        }

        holder.txtTitle.setText(ActionAlertsFragment.title[position]);
        holder.txtPubDate.setText(ActionAlertsFragment.pubDate[position]);

        Bitmap bitmap = getBitmapFromURL(ActionAlertsFragment.thumb[position]);
        if(bitmap == null) {
            holder.thumbnail.setImageResource(R.drawable.kyfb);
        }
        else {
            holder.thumbnail.setImageBitmap(bitmap);
        }

        holder.txtTitle.setTextColor(Color.WHITE);
        holder.txtPubDate.setTextColor(Color.WHITE);

        return convertView;
    }

    public Bitmap getBitmapFromURL(String imageUrl) {
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    static class ViewHolder {
        TextView txtTitle, txtPubDate;
        ImageView thumbnail;
        RelativeLayout lytItemFeed;
    }
}

2 个答案:

答案 0 :(得分:1)

这不会直接回答您的问题,但您可能需要考虑使用图片加载库,例如PicassoVolley。从网络加载图像时有很多复杂性 - 有些微妙,有些不是很多 - 当你在ListView中尝试这样做时更是如此。这两个库(以及其他几个库)都会为您处理所有或大部分复杂性。

答案 1 :(得分:1)

您应该在AsyncTask中移动方法。你正在主线程上下载东西。