ByteArrayOutputStream baos=new  ByteArrayOutputStream(size);
                myBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
                //byte[]result =baos.toByteArray();               
                ContentValues values = new ContentValues();
                DatabaseHelper dbHelper=new DatabaseHelper(context);
                 db=dbHelper.getWritableDatabase();
                db.beginTransaction();
                values.put("BITMAP", baos.toByteArray());
                db.update("TB_CONTACT", values, "ID=?", new String[]{String.valueOf(contact.getId())});
//                if(db.insert("TB_USERLOGIN", null, values) != -1){
//                    Log.d("wjp","chengong");
                mContent=baos.toByteArray();
                db.setTransactionSuccessful();
            }catch (Exception e){
                e.printStackTrace();
            }finally{
                if (db != null) {
                    if (db.inTransaction()) {
                        db.endTransaction();
                    }
                    db.close();
                }一直插不进去  没有数据  谁碰过类似的情况? 图片已经转成二进制了

解决方案 »

  1.   

    首先在库把图片定义成BLOB型,然后再把图片转换成Byte数组后,插入无问题啊
      

  2.   


    在数据库中定义了 一个字段 BLIB类型   你说的把图片定义成bloB有没有例子?
    我是在定义的时候是这样的定义的
    private byte[] mContent;
    private Bitmap myBitmap; 
      

  3.   

    db.execSQL("create table if not exists "
    + TABLE_IMG_INFO
    + "(name text, startDate text, endDate text, type Integer, url text, image blob)");创建表的时候把image这个字段设置成blob类型的,,这样你的image字段就能存入bytes[]了
      

  4.   

    表  private static final String CONTACT_TABLE_CREATE = "create table TB_CONTACT ("
                
                + "BITMAP blob,"//图片
               ";
    不行  
      

  5.   

    你是存入的byte[] 数据吗?
    我都用过无数次的,,怎么会不行了,,你说的不行,是建表报错,,还是不能插入byte[] 数据?
      

  6.   

    哥们,还没解决呢!!!
    接源码:
    SQLiteOpenHelper.javapackage com.wo;
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;public class MySQLiteOpenHelper extends SQLiteOpenHelper {    public static final String DATABASE_NAME = "db";
        public static final int VERSION = 1;
        public static final String TABLE_NAME = "wo";    public MySQLiteOpenHelper(Context context) {
            super(context, DATABASE_NAME, null, VERSION);
        }    @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            String sql = "create table " + TABLE_NAME
                    + "(id intger primary key,text text,image  blob)";
            db.execSQL(sql);
        }    @Override
        public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
            // TODO Auto-generated method stub
            Log.v("Himi", "onUpgrade");
            
        }
       }
    SqliteActivity.javapackage com.wo;import java.io.ByteArrayOutputStream;import android.app.Activity;
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;public class SqliteActivity extends Activity implements OnClickListener {
        private Button insert, query, delete;
        private MySQLiteOpenHelper myOpenHelper;
        private SQLiteDatabase mysql;
        private ImageView imageView;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);        myOpenHelper = new MySQLiteOpenHelper(this);
            prepareView();    }    public void prepareView() {
            imageView=(ImageView) this.findViewById(R.id.imageView1);
            
            insert = (Button) this.findViewById(R.id.insert);
            query = (Button) this.findViewById(R.id.query);
            delete = (Button) this.findViewById(R.id.delete);        insert.setOnClickListener(this);
            query.setOnClickListener(this);
            delete.setOnClickListener(this);
        }    @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            mysql = myOpenHelper.getWritableDatabase();
            switch (view.getId()) {
            case R.id.insert:
                ContentValues cv = new ContentValues();
                cv.put("id", "1");
                cv.put("text", "text1");            Bitmap bimtap = BitmapFactory.decodeResource(this.getResources(),
                        R.drawable.btn_gb_n);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bimtap.compress(Bitmap.CompressFormat.PNG, 100, baos);
                byte[] result = baos.toByteArray();
                cv.put("image", result);            mysql.insert(MySQLiteOpenHelper.TABLE_NAME, null, cv);
                break;
            case R.id.query:
                Cursor cur = mysql.rawQuery("SELECT * FROM "
                        + MySQLiteOpenHelper.TABLE_NAME, null);
                if (cur != null) {
                    int id;
                    String text;
                    byte res[];
                    while (cur.moveToNext()) {// 直到返回false说明表中到了数据末尾
                        id = cur.getInt(cur.getColumnIndex("id"));
                        text = cur.getString(cur.getColumnIndex("text"));
                        res=cur.getBlob(cur.getColumnIndex("image"));
                        Bitmap bitmap=BitmapFactory.decodeByteArray(res, 0, res.length);
                        imageView.setImageBitmap(bitmap);
                        Log.v("tag", "id:" + id + " " + "text:" + text);
                    }
                }
                break;
            case R.id.delete:
                break;
            }
        }
    }
      

  7.   

    你看我的 @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
            ContentResolver resolver=getContentResolver();
            if(requestCode ==0){ 
    //            SQLiteDatabase db = null;
                
                try{
                    
                    Uri originalUri=data.getData();
                    mContent= readStream(resolver.openInputStream(Uri.parse(originalUri.toString())));
                    myBitmap=getPicFromByTes(mContent,null);
                    contactAvatar.setImageBitmap(myBitmap);
    //                int size = myBitmap.getWidth()*myBitmap.getHeight()*4;
                }catch (Exception e){
                    System.out.print(e.getMessage());
                }        }else if(requestCode== REQUEST_CAMERA){
    //            SQLiteDatabase db=null;
                try{
                Log.v("AddContactActivity", "REQUEST_CAMERA=");
                    super.onActivityResult(requestCode, resultCode, data); 
                    Bundle extras=data.getExtras();
                    myBitmap=(Bitmap)extras.get("data");
                    int size = myBitmap.getWidth()*myBitmap.getHeight()*4;
                    ByteArrayOutputStream baos=new  ByteArrayOutputStream(size);
                    myBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
                    ContactDao.saveContactPhoto(this, baos.toByteArray(),contact.getUri());        
                    Log.v("AddContactActivity", "REQUEST_CAMERl=");
                    mContent=baos.toByteArray();
     
                    
                }catch (Exception e){
                    e.printStackTrace();
                }            contactAvatar.setImageBitmap(myBitmap)红色被调用的 //插入头像
        public static boolean saveContactPhoto(Context context,byte[] bitmapByte,String uri)
        {
            SQLiteDatabase db=null;
            try{
                DatabaseHelper helper = new DatabaseHelper(context);
                db = helper.getWritableDatabase();
                db.beginTransaction();            ContentValues cv = new ContentValues();
                cv.put("BITMAP", bitmapByte);
                db.update("TB_CONTACT", cv, "ID=?", new String[] {
                    uri
                });
    //            db.update("TB_CONTACT", cv, "ID=?", new String[]{String.valueOf(contact.getId())});
                db.setTransactionSuccessful();
                return true;
            }
            catch (Exception ex) {
                ex.printStackTrace();
                return false;
            } finally {
                if (db != null) {
                    if (db.inTransaction()) {
                        db.endTransaction();
                    }
                    db.close();
                }
            }
        }
    反正就是插不进去! 唉