从SQLite到ListView的getBitmap

时间:2012-08-17 11:23:51

标签: android sqlite listview bitmap

我正在尝试在Android应用中显示来自Contact的个人资料SQLite图片,但它不起作用,并且会在显示image.setImageBitmap(bitmap) .NullPointerException的{​​{1}}上崩溃不知道为什么。

如果有人可以帮助我。这是我的代码:

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

    DatabaseHandler helper = new DatabaseHandler(this);
    database = helper.getWritableDatabase();
    Cursor data = database.query("amigos", fields, null, null, null, null, null);
    data.moveToFirst();
    dataSource = new SimpleCursorAdapter(this, R.layout.row_def, data, fields, new int[] {R.id.Nombre_Amigo, R.id.profile_picture});

    Log.d("ListarContactos","Oncreate....data: "+data.toString());

        while (!data.isAfterLast()){    
            byte[] bb = data.getBlob(data.getColumnIndex(DatabaseHandler.KEY_PIC_SQ));
            Log.d("ListarContactos","Oncreate....bb: "+bb.toString());
            ByteArrayInputStream inputStream = new ByteArrayInputStream(bb);
            Log.d("ListarContactos","Oncreate....inputStream: "+inputStream.toString());
            Bitmap bitmap = BitmapFactory.decodeByteArray(bb, 0, bb.length);
            Log.d("ListarContactos","Oncreate....Bitmap: "+bitmap.toString());
            ImageView image = (ImageView) findViewById(R.id.profile_picture);
            image.setImageBitmap(bitmap);
            Log.d("ListarContactos","Oncreate....image: "+image.toString());
        }

    Log.d("ListarContactos","Oncreate....datasource: "+dataSource.toString());      
    setListAdapter(dataSource);
    Log.d("ListarContactos","Oncreate....despres del ListAdapter");     
    helper.close();
    ListView view = getListView();
}

1 个答案:

答案 0 :(得分:0)

它会抛出nullPointerException,因为方法BitmapFactory.decodeByteArray(bb, 0, bb.length)会将NULL返回到位图。

相关问题