- Home /
loading image as a blob from Sqlite,loading image from Sqlite
Hi guys, this is my first time using sqlite, i managed to display the text, but the image shows a red question mark, which means that the texture couldn't be retrieved, however the byte array is not empty, so i dont know what to do.
Here is my code for reading the database :
private void readQuestionsFromDB(){
string conn = "URI=file:" + Application.dataPath + "/quizdb.s3db"; //Path to database.
IDbConnection dbconn;
dbconn = (IDbConnection) new SqliteConnection(conn);
dbconn.Open(); //Open connection to the database.
IDbCommand dbcmd = dbconn.CreateCommand();
string sqlQuery = "SELECT id, statement, answer, image " + "FROM questions";
dbcmd.CommandText = sqlQuery;
IDataReader reader = dbcmd.ExecuteReader();
while (reader.Read())
{
string statement = reader.GetString(1);
answerint = reader.GetInt32(2);
byte[] img = (byte[])reader["image"];
Question q = new Question(statement, answer, img);
questions.Add(q);
}
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbconn.Close();
dbconn = null;
}
And here is how i try to display it as an image in the start method :
readQuestionsFromDB();
statement.text = questions[0].statement;
Texture2D tex = new Texture2D(800,400); //image is 800/400
tex.LoadImage(questions[0].image);
image.GetComponent<Image>().sprite = Sprite.Create(tex, new Rect(0,0,tex.width,tex.height),new Vector2(0.5f, 0.5f));
Here is what i get, the text displays correctly, but not the image :
Please help me, i have looked everywhere and can't find a solution, thank you in advance!
I know its not what you asked but: Is there any reason you need to store the image in the database? Its generally considered bad form (unless you are dealing with a lot of data that needs to be directly queryable). Can you not ins$$anonymous$$d just store the path to it and then store it on disk?
That said whats your code for writing them into the database?
Answer by Bunny83 · Jan 29, 2020 at 03:59 PM
What kind of "image" did you store in your database? You know that Unity can only load jpg or png files natively at runtime.
Besides that what have you already done to debug your issue? Have you checked if whatever you get back from reader["image"]
is actually not null and that the byte array has a non zero size? Likewise tex.LoadImage now returns a bool if the loading was successful or not.
ps: There's no need / point in creating an empty Texture that is that large when you use LoadImage. The whole content is replaced by the loaded image (if the loading is successful or course). The "question mark" texture generally indicates an error when loading the image. So either your data is corrupted in some way (no data, wrong data) or you use a non supported texture format to begin with.
Note that for any other image format you have to do the decoding / loading yourself. The Unity engine does not include any loaders for all those file formats which the Untiy editor supports. It would blow up the size of your build and also might cause licencing issues with the used loading code.
If that's your actual issue that you want to load a format not supported by Unity, have a look at this question. You may also want to have a look at my Utilities repository. I haven't really worked on those for quite some time. Though I have a working B$$anonymous$$P and GIF loading routine. Though while the B$$anonymous$$P loader can give give you a Texture2D, the Gif loader does not (yet) directly outputs a Texture2D. Especially since GIF could contain multiple layers / frames which might be animated. Each GIFImageBlock has a DrawTo method which allows you to draw that frame to a Color32 array which you can then convert to a Texture2D.
Since my image loaders are not quite finished yet they are inside the WorkInProgress folder. If the file you try to load has any other format, I'm sorry but I don't have a solution for that. In the past I saw a paid asset on the assetstore which provides a huge plugin for loading all sorts of image formats. I think it was based on some common C++ library. Though currently I can't really find anything useful in the store.
i checked the size of the array, and also the boolean returned by : tex.LoadImage, here is the result :
statement.text = questions[0].statement;
Texture2D tex = new Texture2D(2,2);
byte[] byt = questions[0].image;
tex.LoadImage(byt);
Debug.Log("array size = "+byt.Length);
Debug.Log("tex load image " +tex.LoadImage(byt));
tex.Apply();
image.GetComponent<Image>().sprite = Sprite.Create(tex, new Rect(0,0,800,400),new Vector2(0f, 0f));
][1]
The image file is .jpeg , i guess it's the same as jpg??
When i changed the size of the texture to :
Texture2D tex = new Texture2D(2,2);
and this to display the image :
image.GetComponent<Image>().sprite = Sprite.Create(tex, new Rect(0,0,800,400),new Vector2(0f, 0f));
i get an error : ArgumentException: Could not create sprite (0, 0, 800, 400) from a 8x8 texture. UnityEngine.Sprite.Create (UnityEngine.Texture2D texture, UnityEngine.Rect rect, UnityEngine.Vector2 pivot, System.Single pixelsPerUnit, System.UInt32 extrude, UnityEngine.Sprite$$anonymous$$eshType meshType, UnityEngine.Vector4 border, System.Boolean generateFallbackPhysicsShape) (at :0)
If it's a valid jpg / jpeg it would work. $$anonymous$$ay I ask If you can actually share the image you use? You could temporarily add this line to your code to save the byte array to a file. First you should check if you can actually open that file with an image viewer / editor.
System.IO.File.WriteAllBytes("C:\\Testimage.jpg", byt);
This should create a file on your C:\ drive called Testimage.jpg. Try open it. If it doesn't work you might want to share it here so we can have a look at it.
ps: The return value of LoadImage is not reliable as you can read here and here. Strangely Unity closed the issue with the answer "by design" which is I$$anonymous$$HO a horrible design. In the past people always wanted to know when the loading of an image failed and the only solution back then was to compare that 8x8 question mark image manually. It seems that is still the case.
So I'm pretty sure your image is either not a jpeg or it is corrupted in some way. How did you actually "put them in" your database?
Your answer
Follow this Question
Related Questions
Storing blob in SQLite 1 Answer
Array to blob 0 Answers
How to save blob in bytes to sqlite 0 Answers
Image showing as question mark after loading from SQLite database 0 Answers
LoadImage not loading full image,LoadImage not working properly 0 Answers