A blob refers to a binary large object. A BLOB usually comes into picture with Sqlite, ususally it is some data(image, audio file) which needs to be stored in a database purely on need basis. Although it is not a very good approach to do this but some times it is just the requirement. So actually, I demonstrate here to store an icon image into the android sqlite as a BLOB(byte array) and then retrieve and show it on the screen.
- Step 1 would be to convert the image into a byte array,
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos); byte[] bitmapdata = bos.toByteArray();
- Step 2 would be to store this in the database,
SQLiteDatabase db=this.openOrCreateDatabase(“imagedatabase”, this.MODE_PRIVATE, null); db.execSQL(“CREATE TABLE IF NOT EXISTS imagetable (“ + “_id INTEGER PRIMARY KEY AUTOINCREMENT,” + “image BLOB” + “);”); ContentValues values = new ContentValues(); values.put(“image”, bitmapdata); long row_id=db.insert(“imagetable”, null, values);
- Step 3 would be to retrieve and display it back on screen,
Cursor cursor = db.query(“imagetable”, new String[]{“image”}, null, null, null, null,null); System.out.println(“—–getcolumn count”+cursor.moveToFirst()); //get it as a ByteArray byte[] mybyte=cursor.getBlob(0); //the cursor is not needed anymore cursor.close(); //convert it back to an image ByteArrayInputStream imageStream = new ByteArrayInputStream(mybyte); Bitmap theImage = BitmapFactory.decodeStream(imageStream); ((ImageView)findViewById(R.id.view_image)).setImageBitmap(theImage);
A simple 3 step process!
but how to store video file sqlite database , in column which having blob type
Hello, i think that i saw you visited my site thus i came
to “return the favor”.I am attempting to find things to enhance my website!
I suppose its ok to use some of your ideas!!
Hi
I got a question, is it a method which i can directly move information from a huge file to a database BLOB? I ask this question because i guess in some occasion file can not be fitted in system RAM.
If the file does not fit into the RAM, you can use a piece by piece copy.
Hello, what about storing an mp3 in database?
Never really tried that, But i think it can be converted to a stream of bytes too and hence can be stored as a blob.
Thanks for the reply..But can you give an example code, maybe it will help me.
Hi! very good tutorial!
I wanted to ask for your help please!
I need the same example but with audio, the audio I have already stored in sqlite database, but when you get it back, I need to play through the mediaplayer class (or any other that allows me to do so) through a button playaudio method call, but not how to put the path in setDataSource (), my code is as follows (sorry my english, but I speak Spanish):
public void playAudio(View view){
byte[] byteSound = noteDAO.getAudioByTitle(txtTituloString);
ByteArrayInputStream soundStream = new ByteArrayInputStream(byteSound);
Bitmap theSound = BitmapFactory.decodeStream(soundStream);
//MediaStore.Audio.AudioColumns.DATA
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(theSound.toString()); /* ??? I don’t know how to put the path*/
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
Log.e(“audio”, “error playing!”);
}
}
It is a requirement to use sqlite, so I store the audio file in a database
Sqlite storage for mp3 file(s) is not recommended. However, if you are absolutely hell-bent on doing it, then i suggest you extract the file from db as byte array and then then pass that byte array to method below:
private void playMp3(byte[] mp3SoundByteArray) {
try {
// create temp file that will hold byte array
File tempMp3 = File.createTempFile(“kurchina”, “mp3”, getCacheDir());
tempMp3.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempMp3);
fos.write(mp3SoundByteArray);
fos.close();
// Tried reusing instance of media player
// but that resulted in system crashes…
MediaPlayer mediaPlayer = new MediaPlayer();
// Tried passing path directly, but kept getting
// “Prepare failed.: status=0x1”
// so using file descriptor instead
FileInputStream fis = new FileInputStream(tempMp3);
mediaPlayer.setDataSource(fis.getFD());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException ex) {
String s = ex.toString();
ex.printStackTrace();
}
}
Also, Please do not use: “Bitmap theSound = BitmapFactory.decodeStream(soundStream);”
This is not for sound files but for images/bitmaps.