Android 通过ContentValues处理Blob数据类型
我正在尝试在Sqlite Db中存储和检索图像数据 为此,我首先在本地设备内存中存储了一个示例pic(路径:storage/emulated/0/Download/) 我将onCreateLoader方法定义如下:Android 通过ContentValues处理Blob数据类型,android,sqlite,android-sqlite,Android,Sqlite,Android Sqlite,我正在尝试在Sqlite Db中存储和检索图像数据 为此,我首先在本地设备内存中存储了一个示例pic(路径:storage/emulated/0/Download/) 我将onCreateLoader方法定义如下: public Loader<Cursor> onCreateLoader(int id, Bundle args) { // Define a projection that specifies the columns from the table we care
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// Define a projection that specifies the columns from the table we care about.
String[] projection = {
InventoryContract.ProductEntry._ID,
InventoryContract.ProductEntry.COLUMN_PRODUCT_PIC,
InventoryContract.ProductEntry.COLUMN_PRODUCT_PRICE,
InventoryContract.ProductEntry.COLUMN_PRODUCT_QTY,
InventoryContract.ProductEntry.COLUMN_PRODUCT_NAME};
// This loader will execute the ContentProvider's query method on a background thread
return new CursorLoader(this,
InventoryContract.ProductEntry.CONTENT_URI,
projection,
null,
null,
null);
}
当我尝试执行此代码时,一切正常,但我看到的是一张空白图像,而不是所选的pic和placer pic
因此,我认为在数据库中插入数据存在一些问题
我正在尝试在Sqlite Db中存储和检索图像数据
我不建议这样做。将图像存储在文件中。将数据存储在标识文件的行中
然后我设置了一个insert方法,用这些示例数据向db提供数据
您正在
列\u PRODUCT\u PIC
中存储一个字符串。您没有存储字节[]
。相对于我的推荐,这是好的。这相对于您的数据检索代码来说是不好的,您正在尝试检索一个字节[]
这不是Andorid Studio的问题。不要使用Android Studio标签!根据您的建议,我更改了一些检索字符串的代码:string prodPathPic=cursor.getString(picColumnIndex)并使用Drawable.createFromPathMethod在图像中转换它,但我在权限方面仍然存在一些问题。但这是另一个故事。。。Thanks@akstavrh:应返回SAMPLE\u IMAGE\u PATH
的值。您可以将其传递到您最喜欢的图像加载库(例如,Glide、毕加索)。
private void insertProduct() {
// Create a ContentValues object where column names are the keys,
// and sample attributes are the values.
ContentValues values = new ContentValues();
values.put(InventoryContract.ProductEntry.COLUMN_PRODUCT_NAME, sampleName);
values.put(InventoryContract.ProductEntry.COLUMN_PRODUCT_QTY, sampleQty);
values.put(InventoryContract.ProductEntry.COLUMN_PRODUCT_PRICE, SamplePrice);
values.put(InventoryContract.ProductEntry.COLUMN_EMAIL, sampleMail);
values.put(InventoryContract.ProductEntry.COLUMN_PHONE, samplePhone);
values.put(InventoryContract.ProductEntry.COLUMN_PRODUCT_PIC, SAMPLE_IMAGE_PATH);
//insert a new row
Uri newUri = getContentResolver().insert(InventoryContract.ProductEntry.CONTENT_URI,values);
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// Define a projection that specifies the columns from the table we care about.
String[] projection = {
InventoryContract.ProductEntry._ID,
InventoryContract.ProductEntry.COLUMN_PRODUCT_PIC,
InventoryContract.ProductEntry.COLUMN_PRODUCT_PRICE,
InventoryContract.ProductEntry.COLUMN_PRODUCT_QTY,
InventoryContract.ProductEntry.COLUMN_PRODUCT_NAME};
// This loader will execute the ContentProvider's query method on a background thread
return new CursorLoader(this,
InventoryContract.ProductEntry.CONTENT_URI,
projection,
null,
null,
null);
}
public void bindView(View view, Context context, Cursor cursor) {
// Find individual views that we want to modify in the list item layout
TextView nameTextView = (TextView) view.findViewById(R.id.prod_name);
TextView priceTextView = (TextView) view.findViewById(R.id.prod_price);
TextView qtyTextView = (TextView) view.findViewById(R.id.prod_qty);
ImageView prodImageView = (ImageView) view.findViewById(R.id.prod_img);
// Find the columns of attributes that we're interested in
int nameColumnIndex = cursor.getColumnIndex(InventoryContract.ProductEntry.COLUMN_PRODUCT_NAME);
int priceColumnIndex = cursor.getColumnIndex(InventoryContract.ProductEntry.COLUMN_PRODUCT_PRICE);
int qtyColumnIndex = cursor.getColumnIndex(InventoryContract.ProductEntry.COLUMN_PRODUCT_QTY);
int picColumnIndex = cursor.getColumnIndex(InventoryContract.ProductEntry.COLUMN_PRODUCT_PIC);
// Read the attributes from the Cursor for the current product
String prodName = cursor.getString(nameColumnIndex);
Double prodPrice = cursor.getDouble(priceColumnIndex);
int prodQty = cursor.getInt(qtyColumnIndex);
byte [] prodImg = cursor.getBlob(picColumnIndex);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[1024 * 32];
Bitmap bmp = BitmapFactory.decodeByteArray(prodImg, 0, prodImg.length, options);
//Update Views
nameTextView.setText(String.valueOf(prodName));
priceTextView.setText(prodPrice.toString());
qtyTextView.setText(String.valueOf(prodQty));
prodImageView.setImageBitmap(bmp);
}
}