I want to store blob data into sqlite, It adds imageData as null although bloc has value. What should I put bloc value into sql?
String x = getResultAsOnlyValue("soapImage", xdata);
byte[] bloc = Base64.decode(x, Base64.DEFAULT);
Bitmap bmp = BitmapFactory.decodeByteArray(bloc,0,bloc.length);
SQLiteStatement statement = db.compileStatement("insert into ImageTable (imageData) values(?)");
statement.bindBlob(0, bloc);
statement.execute();
EDITED: ANSWER TO THE QUESTION
String x = getResultAsOnlyValue("soapImage", xdata);
byte[] bloc = Base64.decode(x, Base64.DEFAULT);
Bitmap bmp = BitmapFactory.decodeByteArray(bloc,0,bloc.length);
SQLiteDatabase db = getDb();
ContentValues values = new ContentValues();
values.put("ImageData", bloc);
db.insert("imageTable", null, values);
After this operation if you get NS_ERROR_FILE_CORRUPTED error while trying to run the db via sqlitemanager that means you need to upgrade your db to sqlite3. In this link it tells you to how to do this.
Good morning, try this answer out: Convert Drawable to BLOB Datatype
I think it's what you're looking for.
Related
It was working very well. But when i put image broked. Before doesn´t exist "foto". But I need save a property type BLOB. But I dont´t know why is broking.
Bitmap bitmap = imageViewProeto.getDrawingCache();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
if(bitmap != null){
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
}
//byte[] data = baos.toByteArray();
byte[] data = stream.toByteArray();
bitmap.recycle();
boolean isOk = validarCampos();
if(!isOk){
String query = "INSERT INTO projetos (nome, descricao, url, foto, voluntario_id ) " +
"VALUES ('"+ nomeDigitado +"','"+descricaoDigitada+ "','"+urlDigitada+"', "+ data +" , " + testeId + ")";
bancoDados.execSQL(query);
A blob needs to be inserted as a string of hex numbers enclosed in single quotes and prefixed by x e.g.
"INSERT INTO projetos (nome, descricao, url, foto, voluntario_id ) " +
"VALUES ('"+ nomeDigitado +"','"+descricaoDigitada+ "','"+urlDigitada+"', x'000102030405060708090a0b0c' , " + testeId + ")";
x'000102030405060708090a0b0c' is just an example of the format using some arbritary value that would very likely not be an image.
As such you'd need to convert the byte[] to the respective value. However, if you use the SQLiteDatabase insert convenience method rather then execSQL then the conversion is performed by the method and thus the value can be passed as a byte[].
e.g. you could use :-
ContentValues() cv = new ContentValues();
cv.put(nome,nomeDigitado);
cv.put(nomeDigitado,descricaoDigitada);
cv.put(foto,data);
cv.put(voluntario_id,testeId);
cv.put(url,urlDigitada); //<<<<< note out of order but the correct SQL will be built
long rowid = bancoDados.insert("projetos",null,cv);
In addition to conveniently handling byte[]'s the insert method also has the fowlling advantages :-
it returns the rowid of the inserted row or -1 if the row was not inserted.
it also offers protection against SQL Injection.
it builds the underlying SQL (for insertion via VALUES)
HOWEVER, if the byte[] is around 2MB or greater then you will not be able to retrieve the BLOB as there is a 2MB size limit for a Cursor Window (a buffer into which complete rows are placed). There may also be issues with smaller byte[] as the process for retrieving the data can be relatively slow.
It is recommended to rather save the path to the respective image which is stored as a file. However, saving images (byte[]) as BLOBs that are around about 100KB can be more efficient.
I have a Db where I have stored the path of a video and its thumbnail in ByteArray form. But when I try to retrieve the thumbnail from the DB I always get null value. Here is the code I am using to retrieve both the thumb and the path:
db=openOrCreateDatabase("VideoPaths", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS paths(path VARCHAR,thumbnail BLOB);");
Cursor c = db.rawQuery("SELECT path,thumbnail FROM paths",null);
int index = c.getColumnIndex("path");
int index2 = c.getColumnIndex("thumbnail");
c.moveToFirst();
if (c != null) {
// Loop through all Results
do {
String VideoPath = c.getString(index);
byte[] bytes = c.getBlob(index2);
Bitmap thumb = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
pathList.add(VideoPath);
listRow.add(new VideoRow(VideoPath,thumb, "User", "Testing"));
} while (c.moveToNext());
}
Do you have any ideas on how to fix this issue ?
P.S The video path retrieval is working fine.
EDIT: Here is the code that populates the table
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath,
MediaStore.Images.Thumbnails.MINI_KIND);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
thumb.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] thumbnailBitmapBytes = stream.toByteArray();
db=openOrCreateDatabase("VideoPaths", Context.MODE_PRIVATE, null);
db.execSQL("INSERT INTO paths (path,thumbnail) values('" + serverPath + "','"+thumbnailBitmapBytes+"')");
First of all, I would not construct your INSERT query by concatenating strings. Take a look at database.compileStatement(...) here. Try doing this:
SQLiteStatement stmt = db.compileStatement("INSERT INTO paths (path, thumbnail) values (?, ?)");
stmt.bindString(1, serverPath);
stmt.bindBlob(2, thumbnailBitmapBytes);
long newRowId = stmt.executeInsert();
Then, when it's time to fetch your data, you can use the code you've already shared.
I'm storing int,char,float types as byte[] in the SQLite database, the data is getting saved properly, Now how can we query the database to select a particular byte[] from the database using the Android API's for SQLite?
public void retrieveRecord(String firstName)
{
byte[] b = firstName.getBytes();
Cursor cursor = database.rawQuery("select * from myTable where firstName='"+b+"'", null);
}
Inside Database firstName is TEXT type but it is saved as byte[].
Please help me to modify the above code so that I can query on byte[].
the BLOB type may meet your request.
byte[] value = cursor.getBlob(cursor.getColumnIndex(name));
and then use ObjectInputStream to deserialize to object.
I am doing a project on Contacts For this , I need to Import/Store Pictures in SQlite DataBase.From what I have read , I have to convert the picture into a different format (maybe wrong in this assumption) and then store it in the data Base . Can you please tell me how can I do it or can I use a different method to do it?Both would be helpful for me , so that I can learn a bit more.
Thank You,
Depending on where you want to get the picture from, whether from the internet or from the sdcard. However, I'm assuming that you can get it and convert it into a bitmap.
Bitmap bm = (get it from anywhere);
ByteArrayOutputStream blob = new ByteArrayOutputStream();
//the below line is used if you like to scale it down to store it into the database
bm = Bitmap.createScaledBitmap(bm, width, height, false);
//then put it into a byte array to store it in the database
bm.compress(CompressFormat.JPEG, 100, blob);
// method call to database
dbClass.addPicture(blob.toByteArray());
Table definition in the database:
SQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS Pictures (id integer primary key autoincrement, pic blob not null ");
addPicture method in the database class is as the following
public void addPicture(byte[] picture){
db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("pic", picture);
db.insert("Pictures ", null, cv);
}
I hope that helps
I'm doing Android development.
My question is: can I append a specific field of a db record to a URI?
Example: suppose you have a table monuments in a SQLite DB:
_id INTEGER AUTO INCREMENT
name TEXT
location TEXT
picture BLOB
I want now to retrieve the BLOB image of entry with ID 4 and display it; I can query the db
Bitmap monumentImage = null;
Uri uri = Uri.parse("content://com.my.example/monuments/4");
Cursor c = myContentResolver.query(uri, new String[] {"picture"}, null, null, null);
if (c.moveToFirst()) {
byte[] imageBytes = c.getBlob(0);
if (imageBytes != null) {
monumentImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
}
ImageView myImage = findViewById(R.id.whatever);
if (monumentImage != null) {
myImage.setImageBitmap(monumentImage);
}
now my question is: is there a way to append directly the record column to the uri?
as in, having something like
Uri uri = Uri.parse("content://com.my.example/monuments/4/picture");
ImageView myImage = findViewById(R.id.whatever);
myImage.setImageURI(uri);
?
Cheers!
Yep. Just use own content provider. There you will be able to parse the URI. Read here, the
Making the Query
section.