akstavrh
akstavrh

Reputation: 63

Handle Blob data type through ContentValues

I am trying to store and retrieve image data in Sqlite Db.

To do so I firstly stored in local device memory an example pic (path: storage/emulated/0/Download/).

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    private final String SAMPLE_IMAGE_PATH = "/storage/emulated/0/Download/image.jpg";

Then I set up an insert method to feed the db with these example data:

 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);
}

and I define the onCreateLoader method as follows:

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);                  
}

In the CursorAdapter class I updated the listView adding the data from db in bindView() method:

 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);
}
}

When I try execute this code everything goes ok, but I see a blank image instead of both the selected pic and placer pic.

So I think that there is some problem with inserting data into db.

Upvotes: 1

Views: 483

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007340

I am trying to store and retrieve image data in Sqlite Db

I do not recommend this. Store the images in files. Store data in the rows that identifies the files.

Then I set up an insert method to feed the db with these example data

You are storing a string in COLUMN_PRODUCT_PIC. You are not storing a byte[]. This is good, relative to my recommendation. This is bad relative to your data-retrieval code, where you are attempting to retrieve a byte[].

Upvotes: 2

Related Questions