Converting String image in SQLite back to image to display on grid view

8
March 16, 2019, at 11:10 AM

Database initialization:

private final static String DBNAME = "Auction";
private final static int DBVERSION = 2;
SQLiteDatabase mDB5;
public final static String TBL_AUCTION = "Auction";
public final static String COL_AUCTION_ID = BaseColumns._ID;
public final static String COL_AUCTION_NAME = "name";
public final static String COL_AUCTION_DESCRIPTION = "description";
public final static String COL_AUCTION_PRICE = "price";
public final static String COL_AUCTION_DURATION = "duration";
public final static String COL_AUCTION_IMAGE = "image";

private String crt_tbl_auction = "CREATE TABLE IF NOT EXISTS " + TBL_AUCTION + "(" +
        COL_AUCTION_ID + " INTEGER PRIMARY KEY, " +
        COL_AUCTION_NAME + " TEXT, " +
        COL_AUCTION_DESCRIPTION + " TEXT, " +
        COL_AUCTION_PRICE + " TEXT, " +
        COL_AUCTION_DURATION + " TEXT, " +
        COL_AUCTION_IMAGE + " BLOB " +
        ")";
public DatabaseHelper5(Context context) {
    super(context, DBNAME, null, DBVERSION);
    mDB5 = this.getWritableDatabase();
}

insertData:

public long insertData(String name, String description, String price, String duration,byte[] image) {
    ContentValues cv = new ContentValues();
    cv.put(COL_AUCTION_NAME, name);
    cv.put(COL_AUCTION_DESCRIPTION, description);
    cv.put(COL_AUCTION_PRICE, price);
    cv.put(COL_AUCTION_DURATION, duration);
    cv.put(COL_AUCTION_IMAGE,String.valueOf(image));
    return mDB5.insert(TBL_AUCTION, null, cv);
}

getData:

public Cursor getDataV2() {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] projection = {
                COL_AUCTION_ID,
                COL_AUCTION_IMAGE,
                COL_AUCTION_NAME
        };
    return db.query(
                TBL_AUCTION,   // The table to query
                projection,             // The array of columns to return (pass null to get all)
                null, null, null, null, BaseColumns._ID + " DESC"
        );
}

MainActivity:

gridView = (GridView)findViewById(R.id.gridView);
    list = new ArrayList<>();
    adapter = new AuctionAdapter(this,R.layout.auction_items,list);
    gridView.setAdapter(adapter);
    myDB5 = new DatabaseHelper5(this);

    wholegrid = myDB5.getDataV2();
    if (sma == null) {
        sma = new SimpleCursorAdapter(
                this,
                android.R.layout.simple_list_item_2,wholegrid,
                new String[]{DatabaseHelper5.COL_AUCTION_IMAGE, DatabaseHelper5.COL_AUCTION_NAME},
                new int[]{android.R.id.text1,android.R.id.text2},
                0
        );
        gridView.setAdapter(sma);
    }
    adapter.notifyDataSetChanged();
}

I am having trouble converting my string image back to image and display it on GridView. The reason why I convert it to string was because without String.valueOf, my app will crash with error saying that it could not convert BLOB to string. Is there any way that I could edit my code minimally to solve this issue?I just want it to be able to retrieve the image data and display the image to GridView, that's all. Any method is welcome. Thanks!

READ ALSO
Play whatsapp voice message directly after receiving

Play whatsapp voice message directly after receiving

I'm using NotificationListenerService to identify when a notification receives

17
Duplicate Webview (streaming video)

Duplicate Webview (streaming video)

I'm struggling with problem that I can't figure out by myselfFirst of all, I'm trying to stream video from local server

39
Notification in Oreo working sometimes and sometimes not

Notification in Oreo working sometimes and sometimes not

I am working with notification in Android app

39
How to adjust the text auto-resize &amp; when it moves to the 2nd line

How to adjust the text auto-resize & when it moves to the 2nd line

I am building a meme generatorThe issue that I'm having is with the texts that are drawn on the Canvas

22