Android设置了图像位图问题

时间:2011-10-01 11:03:30

标签: android image bitmap

我正在开发一个与网络服务器通信并下载一些照片的项目。我希望这些照片在下载后显示在列表视图中,但它会给我一个NullPointerException。这是我正在使用的代码:

   private ArrayList <HashMap<String, Object>> items;
    private static final String NAMEKEY = "bookname";
    private static final String INFOKEY = "bookprice";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mystampii_layout);
    }
        });

        ListView listView = (ListView)findViewById(R.id.mystampii_listview);
        items = new ArrayList<HashMap<String,Object>>();
        HashMap<String, Object> hm;


        final Bitmap b = BitmapFactory.decodeFile("/sdcard/MediaCard2a44bb1f782925923195ee546e7ef395.jpg", null);
        ImageView brum = (ImageView) findViewById(R.id.main_img);
        brum.setImageBitmap(b);


        hm = new HashMap<String, Object>();
        hm.put(NAMEKEY, "Moto GP 2010");
        hm.put(INFOKEY, "98 Stampii");
        items.add(hm);

          // Define SimpleAdapter and Map the values with Row view R.layout.listbox
           SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.main_listview,
                    new String[]{NAMEKEY,INFOKEY}, new int[]{R.id.main_img,R.id.main_name, R.id.main_info});

            listView.setAdapter(adapter);
            listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }

我知道,为什么我会收到NullPointerException:因为我的ImageView brum位于R.layout.main_listview,我在此活动R.layout.mystampii_layout上设置为内容。我真的可以理解如何将图像从SDCard设置为另一种布局的ImageView。

有任何建议我该怎么做,或者如何在列表视图中显示SDCard下载的图像。

非常感谢任何帮助!!!

3 个答案:

答案 0 :(得分:1)

尝试简单代码::

 ImageView image = (ImageView) findViewById(R.id.test_image);
        FileInputStream in;
        BufferedInputStream buf;
        try {
            in = new FileInputStream("/sdcard/test2.png");
            buf = new BufferedInputStream(in);
            Bitmap bMap = BitmapFactory.decodeStream(buf);
            image.setImageBitmap(bMap);
            if (in != null) {
            in.close();
            }
            if (buf != null) {
            buf.close();
            }
        } catch (Exception e) {
            Log.e("Error reading file", e.toString());
        }

答案 1 :(得分:1)

实现您所需要的最佳方法是实现自定义适配器并在Adapter.getView()中充气R.layout.main_listview

答案 2 :(得分:0)

像这样创建自定义适配器:

import java.util.ArrayList;

import com.stampii.stampii.R;
import com.stampii.stampii.comm.rpc.CollectionRPCPacket;
import com.stampii.stampii.comm.rpc.UserDatabaseHelper;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyArrayAdapter extends ArrayAdapter<String> {
    private final Activity context;
    private final String[] names;
    CollectionRPCPacket rpcPacket;
    Cursor cursor;
    String title;

    public MyArrayAdapter(Activity context, String[] names) {
        super(context, R.layout.main_listview, names);
        this.context = context;
        this.names = names;
    }

    // static to save the reference to the outer class and to avoid access to
    // any members of the containing class
    static class ViewHolder {
        public ImageView imageView;
        public TextView textView,textView2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // ViewHolder will buffer the assess to the individual fields of the row
        // layout

        ViewHolder holder;
        // Recycle existing view if passed as parameter
        // This will save memory and time on Android
        // This only works if the base layout for all classes are the same
        View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.main_listview, null, true);
            holder = new ViewHolder();
            holder.textView = (TextView) rowView.findViewById(R.id.main_name);
            holder.textView2 = (TextView) rowView.findViewById(R.id.main_info);
            holder.imageView = (ImageView) rowView.findViewById(R.id.main_img);
            rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }
        final Bitmap b = BitmapFactory.decodeFile("/sdcard/MediaCard2a44bb1f782925923195ee546e7ef395.jpg", null);


        holder.textView.setText();
        holder.textView2.setText("");
        holder.imageView.setImageBitmap(b);


        return rowView;
    }
}

您可以在活动中使用它:

ListView lv1 = (ListView)findViewById(R.id.listViewOne);
String[] names = new String[] { "ONE", "TWO", "THREE"};
lv1.setAdapter(new MyArrayAdapter(this, names));

希望它有所帮助!