2015-02-06 86 views
4

我想在我的自定义列表视图中从我的sqlite数据库中显示图像。我成功地转换了一个位图图像数组,并编写了用于转换回位图图像的代码。但图像不是显示off.I正在使用SqliteDatabaseHelper类插入图像,除了图像database.All其他值都displaying.What值我应当增加?请帮Android - 插入图像的位图阵列到SQLite数据库

MainActivity.java

public class MainActivity extends Activity{ 


ListView prd_list; 
public static Integer clas; 
byte [] imge; 

int j; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    prd_list = (ListView) findViewById(R.id.list); 
    DataBaseHandler dbh = new DataBaseHandler(this); 

    /////////converting image to bytearray////// 

    Bitmap[] images = { BitmapFactory.decodeResource(getResources 
      (),R.drawable.candle1),BitmapFactory.decodeResource(getResources 
      (),R.drawable.candl3),BitmapFactory.decodeResource(getResources(),R.drawable.lawn)}; 

    byte[][] img = new byte[images.length][]; 
    for (j=0; j<images.length; j++) { 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     images[j].compress(Bitmap.CompressFormat.PNG, 100, bos); 
     img[j] = bos.toByteArray(); 
    } 
     ////////////////////////////////////// 

    SQLiteDatabase db = dbh.getWritableDatabase(); 
    Cursor cr = db.rawQuery("SELECT * FROM product", null); 
    final String[] pname = new String[cr.getCount()]; 
    String[] price = new String[cr.getCount()]; 

    int i = 0; 
    j=0; 
    while(cr.moveToNext()) 
    { 

     String name = cr.getString(cr.getColumnIndex("pname")); 
     String prprice = cr.getString(cr.getColumnIndex("pprice")); 
     imge=cr.getBlob(cr.getColumnIndex("pimage")); 

     img[j] = imge; 
     pname[i] = name; 
     price[i] = prprice; 
     i++; 
     j++; 

    } 
    ListAdapter adapter = new ListAdapter(this, img,pname, price); 
    prd_list.setAdapter(adapter); 
    } 

ListAdapter。 java

public class ListAdapter extends BaseAdapter { 

private final String[] pname; 
private final String[] price; 
private final byte[] []img; 
int j=0; 

private Context mcontext; 
     public ListAdapter(Context c,byte[][]img,String[] pname,String[] price){ 
    mcontext=c; 
    this.pname=pname; 
    this.price=price; 
    this.img=img; 
} 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return pname.length; 
    } 

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

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     View List; 
     LayoutInflater mLayoutinflater=(LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if(convertView==null){ 
     List=new View(mcontext); 

     List=mLayoutinflater.inflate(R.layout.mylist, parent, false); 
     } 
     else{ 
     List = (View)convertView; 
     } 

     TextView textView1 = (TextView)List.findViewById(R.id.pr_name); 
     TextView textView2 = (TextView)List.findViewById(R.id.pr_price); 
     ImageView imageview= (ImageView)List.findViewById(R.id.pr_img); 
     textView1.setText(pname[position].toString()); 
     textView2.setText("Rs "+price[position] +" /-"); 
     if(img.length>0){ 
     Bitmap b1=BitmapFactory.decodeByteArray(img[j], 0, img[j].length); 
     imageview.setImageBitmap(b1); 
     } 
     // TODO Auto-generated method stub 
     return List; 

    } 

} 

DataBaseHandler.java

public class DataBaseHandler extends SQLiteOpenHelper{ 

public static final int DATABASE_VERSION = 1; 
    public static final String DATABASE_NAME = "products.db"; 
    public static final String CONTACTS_TABLE_NAME = "product"; 
    public static final String CONTACTS_COLUMN_ID = "pid"; 
    public static final String CONTACTS_COLUMN_NAME = "pname"; 
    public static final String CONTACTS_COLUMN_EMAIL = "pspec"; 
    public static final String CONTACTS_COLUMN_STREET = "pprice"; 
    public static final String CONTACTS_COLUMN_CITY = "pfeature"; 
    public static final String CONTACTS_COLUMN_PHONE = "pimage"; 

public DataBaseHandler(Context context) { 
    super(context, DATABASE_NAME, null, 1); 

    // TODO Auto-generated constructor stub 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    // TODO Auto-generated method stub 

    db.execSQL("CREATE TABLE IF NOT EXISTS product(pimage BLOB,pid INTEGER PRIMARY KEY,pname TEXT,pprice NUMERIC,pspec TEXT,pfeature TEXT)"); 
    db.execSQL("INSERT INTO product(pimage,pname,pprice,pspec) VALUES('img[0]','Candle stick 1',4000,'Solar garden/pathway light,Solar Panel:1pc crystal silicon solar cell, Battery:1pc 1.2V Ni-MH/Ni-CD AA battery 600MA ,Material:Stainless steel ,WaterProof and safe ')"); 
    db.execSQL("INSERT INTO product(pimage,pname,pprice,pspec) VALUES('img[1]','Candle stick 3','4500','Solar garden/pathway light, Solar Panel:1pc crystal silicon solar cell, Battery:1pc 1.2V Ni-MH/Ni-CD AA battery 600MA, Material:Stainless steel, WaterProof and safe IP44 ')"); 

    db.execSQL("INSERT INTO product(pimage,pname,pprice,pspec) VALUES('img[6]','Senson',4500,'Solar garden/pathway light, Solar Panel:1pc crystal silicon solar cell, Battery:1pc 1.2V Ni-MH/Ni-CD AA battery 600MA, Material:Stainless steel, WaterProof and safe IP44 ')"); 
    db.execSQL("INSERT INTO product(pimage,pname,pprice,pspec) VALUES('img[7]','Thejus',7500,'Solar garden/pathway light, Solar Panel:1pc crystal silicon solar cell, Battery:1pc 1.2V Ni-MH/Ni-CD AA battery 600MA, Material:Stainless steel, WaterProof and safe IP44 ')"); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // TODO Auto-generated method stub 

} 
public void addEntry(byte[] image) throws SQLiteException{ 
    SQLiteDatabase products = getWritableDatabase(); 
    ContentValues cv = new ContentValues(); 
    cv.put("pimage", image); 
    products .insert(CONTACTS_TABLE_NAME, null, cv); 
} 

public void addEntries(byte[][] img) { 
    for(int i = 0 ; i < img.length ; i++) 
     addEntry(img[i]); 
} 

} 

EDITED

public void addEntry(byte[] image) throws SQLiteException{ 
    SQLiteDatabase products = getWritableDatabase(); 
    ContentValues cv = new ContentValues(); 
    cv.put("pimage", image); 
    products .insert(CONTACTS_TABLE_NAME, null, cv); 
} 

public void addEntries(byte[][] img) { 
    for(int i = 0 ; i < img.length ; i++) 
     addEntry(img[i]); 
} 
+0

'db.execSQL(“INSERT INTO产品(pimage,PNAME,pprice,PSPEC)VALUES( 'IMG [0]''哪里是IMG [0]? – 2015-02-06 04:31:06

+0

我的图像数组是img [j]。我试图访问每个图像使用其索引像img [0],IMG [1] ... – droid 2015-02-06 04:33:29

+0

检查此问题:http://stackoverflow.com/questions/11790104/how-to-storebitmap-image-and-retrieve -image-from-sqlite-database-in-android – ReZa 2015-02-06 04:36:41

回答

0

试试这个在您的适配器来获取图像:

byte[] outImage=img[j]; 
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage); 
Bitmap theImage = BitmapFactory.decodeStream(imageStream); 
imageview.setImageBitmap(theImage); 
+0

您是否添加了imageview.setImageBitmap(theImage);最后 ? – 2015-02-06 04:54:30

+0

拳头你得到任何错误?并且你有其他数据库条目有问题吗? – 2015-02-06 05:07:05

+0

没有得到任何错误,没有其他条目的问题。一切工作正常,除了图像 – droid 2015-02-06 05:25:47

2

保存图像这样的:

public void addEntry(String name, byte[] image) throws SQLiteException{ 
    ContentValues cv = new ContentValues(); 
    cv.put(KEY_NAME, name); 
    cv.put(KEY_IMAGE, image); 
    database.insert(DB_TABLE, null, cv); 
} 

public void addEntries(String[] name , byte[][] img) { 
    for(int i = 0 ; i < name.length ; i++) 
     addEntry(name[i] , img[i]); 
} 
+0

@droid在你的DataBaseHandler类。 – ReZa 2015-02-06 05:05:04

+0

我不熟悉这个sqlite公开课。我在我的代码中更新了你的答案。那是否正确?和我的数据库和表名不能解决。显示错误。 – droid 2015-02-06 05:24:19

+0

更改您的代码,请添加:SQLiteDatabase products = getWritableDatabase();并chnge这行products.insert(product,null,cv);到:products.insert(CONTACTS_TABLE_NAME,null,cv); – ReZa 2015-02-06 05:36:36