Picture data in SQLite

124 views
Skip to first unread message

Syed Munawer Hassan

unread,
Sep 24, 2019, 2:12:24 AM9/24/19
to DroidScript
How Picture Data can be saved in SQLite using DS ?

Steve Garman

unread,
Sep 24, 2019, 4:40:45 AM9/24/19
to DroidScript
// It depends what you want to do with it
// you will need save it as either a string or a blob
// this assumes you eant to save a .png image
// and display it in an image when retrieved
// a string is the easiest answer for that.

var path = "/Sys/Img/Hello.png"
var array, img;

function OnStart()
{
img = app.CreateImage(path);
var str = GetImageString(img);

// when str retrieved from database
var data = JSON.parse( str )
var newImg = app.CreateImage( null, data.width, data.height, "px" );
newImg.SetPixelData( data.png64,data.width,data.height,"px" );
var lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
alert( newImg.GetWidth());
lay.AddChild( newImg );
app.AddLayout( lay );
}

function GetImageString(img)
{
var w = img.GetAbsWidth();
var h = img.GetAbsHeight();
var png = img.GetPixelData("pngBase64", 0, 0, w, h);
var ret = { };
ret.width = w;
ret.height = h;
ret.png64 = png;
return JSON.stringify(ret);
}

Steve Garman

unread,
Sep 24, 2019, 4:49:17 AM9/24/19
to DroidScript
I should point out that the file does not have to have been a .png file in the first place.

pngBase64 is just the way it it encoded from the img control.

I chose it because it is lossless and the new image should be identical to the old.

Syed Munawer Hassan

unread,
Mar 27, 2021, 5:36:50 AM3/27/21
to DroidScript
but i want to take picture from camera then save it to sql in android 10

Alan Hendry

unread,
Mar 27, 2021, 11:38:01 AM3/27/21
to DroidScript
Hi,
To use Steve's solution you need to TakePicture saving the photo to a file,
then use his code (to load the photo into an Image, GetPixelData to convert to a string) then save in DB.
Regards, ah

Reply all
Reply to author
Forward
0 new messages