【问题标题】:Image Bitmap from URL来自 URL 的图像位图
【发布时间】:2014-08-14 12:04:00
【问题描述】:

我正在尝试从字符串数组中的 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;
    }
}

【问题讨论】:

  • 您可能需要提供堆栈跟踪以获得更多有用的帮助。
  • 您是否拥有清单中的权限。你不应该在你的主线程上下载图片。
  • "android os network on main thread exception" ?

标签: java android inputstream


【解决方案1】:

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

【讨论】:

  • 我同意。毕加索是解决这个问题的一个很好的解决方案:)
【解决方案2】:

您应该在 AsyncTask 中移动您的方法。您正在主线程上下载内容。

【讨论】:

    猜你喜欢
    • 2012-08-23
    • 2021-06-11
    • 2019-07-22
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2014-06-04
    相关资源
    最近更新 更多