在SQLite中的Bitmap中存储和检索问题

时间:2014-01-11 07:56:54

标签: java android sqlite bitmap

我在SQLite中存储位图并检索,但图像没有显示原始大小。第一张图片显示为big white Dot(.),第二张图片没有原始尺寸。

这是我存储在数据库中的代码:

public long insertProductCategoryData(String tid, String categoryName,
        Bitmap categoryImageBitmap) {
    SQLiteDatabase sqliteDB = dbHandler.getWritableDatabase();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    categoryImageBitmap.compress(Bitmap.CompressFormat.PNG, 100,
            byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    ContentValues contentValues = new ContentValues();
    Integer tidInteger = Integer.valueOf(tid);
    contentValues.put(DatabaseHandler.TID, tidInteger);
    contentValues.put(DatabaseHandler.PRODUCT_CATEGORY_IMAGE, byteArray);
    contentValues.put(DatabaseHandler.PRODUCT_CATEGORY_NAME, categoryName);
    long id = sqliteDB.insert(DatabaseHandler.PRODUCT_CATEGORY_TABLE, null,
            contentValues);
    return id;
}

从数据库中检索的代码:

public List<ProductCategoryBean> getProductCategoryData() {
    List<ProductCategoryBean> productCategoryBean = new ArrayList<ProductCategoryBean>();
    SQLiteDatabase sqliteDB = dbHandler.getWritableDatabase();
    String[] columns = { DatabaseHandler._TID,
            DatabaseHandler.TID,
            DatabaseHandler.PRODUCT_CATEGORY_NAME,
            DatabaseHandler.PRODUCT_CATEGORY_IMAGE };
    Cursor cursor = sqliteDB.query(DatabaseHandler.PRODUCT_CATEGORY_TABLE,
            columns, null, null, null, null, null);
    if (cursor.getCount() > 0) {

        while (cursor.moveToNext()) {
            ProductCategoryBean prodCatDB = new ProductCategoryBean();
            prodCatDB.set_tid(cursor.getInt(cursor.getColumnIndex(DatabaseHandler._TID)));
            prodCatDB.setTid(String.valueOf(cursor.getInt(cursor
                    .getColumnIndex(DatabaseHandler.TID))));
            prodCatDB.setProductCategoryName(cursor.getString(cursor
                    .getColumnIndex(DatabaseHandler.PRODUCT_CATEGORY_NAME)));
            prodCatDB.setProductCategoryImage(cursor.getBlob(cursor
                    .getColumnIndex(DatabaseHandler.PRODUCT_CATEGORY_IMAGE)));
            productCategoryBean.add(prodCatDB);
        }
    } 
    dbHandler.close();
    return productCategoryBean;
}


List<ProductCategoryBean> selectQueryData = sandiskDatabaseHandler
            .getProductCategoryData();
        dbtids = new String[selectQueryData.size()];
        dblistProCategory = new String[selectQueryData.size()];
        dbimgBitmapUrls = new HashMap<Integer, Bitmap>();
        listOfProductsCategory.setNumColumns(dbtids.length);
    for (ProductCategoryBean data : selectQueryData) {
        dbtids[data.get_tid()-1] = data.getTid();
        dblistProCategory[data.get_tid()-1] = data.getProductCategoryName();
        Bitmap bitmap = BitmapFactory.decodeByteArray(data.getProductCategoryImage() ,0, data.getProductCategoryImage() .length);
        dbimgBitmapUrls.put(data.get_tid(), bitmap);
    }

存储在cacheDir中:

public void saveInternalCache(File folderDir, String fname, Bitmap bitmap) {

    File file = new File(folderDir, fname);

    if (file.exists()) {
        file.delete();
    }
    if ((folderDir.mkdirs() || folderDir.isDirectory())) {
        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
            System.out.println("Stored");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是我调用方法的地方:

     File folderDir= new File(getActivity().getCacheDir(),"myimages/images");
                                                 storeImagesIndevices.saveInternalCache(folderDir,filename,imgBitmapUrls.get(‌​i)); 

0 个答案:

没有答案