Featured post

Marshmallow Features Point by Point

Android Runtime (“ART”) Improved application performance and lower memory overhead for faster  multi-tasking. Battery Doze...

Wednesday 11 February 2015

get bitmap from Blob in sqlite Android

//Call DataBase Class in Activity

SQLiteHelper sqlite = new SQLiteHelper(this);
try {
System.out.println("Check database available or not");
if (!sqlite.checkDataBase()) {
System.out.println("Database available");
sqlite.createDataBase(0, "");
System.out.println("Database Create");
sqlite.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}

// get Bitmap

ByteArrayInputStream imageStream = new ByteArrayInputStream(sqlite.getBlobList().get(0));
Bitmap theImage= BitmapFactory.decodeStream(imageStream);

// Create New Class

package com.example.blobtopng;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SQLiteHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "images.sqlite";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_IMAGES = "data";


private SQLiteDatabase dataBase;


private final Context context;

// Code to copy database from assets start
@SuppressLint("SdCardPath")
public static String DB_PATH = "/data/data/com.example.blobtopng/databases/";

public SQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}

public void createDataBase(int flag, String path) throws IOException {

boolean dbExist = checkDataBase();

if (!dbExist) {
this.getWritableDatabase();

try {
if (flag == 0)
copyDataBase();
else
replaceDataBase(path);

} catch (IOException e) {
Log.d("DataBase Copy :- ", "Not Copy Database");
throw new Error("Error copying database");
}
try {
openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
} else {
if (flag == 1) {
replaceDataBase(path);
}
}
Log.d("DataBaseTrivia Created :-", "Success");

}

public boolean checkDataBase() {

SQLiteDatabase checkDB = null;

try {
String myPath = DB_PATH + DATABASE_NAME;
if (databaseExist()) {
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);

Log.e("checkDataBase Exists :- ",
" Database exists in application");
Log.e("checkDB.getVersion()", checkDB.getVersion() + "");

} else
Log.e("checkDataBase Exists :- ",
" Database not exists in application");

} catch (SQLiteException e) {

Log.d("checkDataBase Exists :- ",
" Database not exists in application");

}

if (checkDB != null) {

checkDB.close();

}

return checkDB != null ? true : false;
}

public boolean databaseExist() {
File dbFile = new File(DB_PATH + DATABASE_NAME);
return dbFile.exists();
}

private void copyDataBase() throws IOException {

InputStream suninputstream = context.getAssets().open(DATABASE_NAME);

String sunFileName = DB_PATH + DATABASE_NAME;

OutputStream sunoutputstream = new FileOutputStream(sunFileName);

byte[] buffer = new byte[1024];

int length;

while ((length = suninputstream.read(buffer)) > 0) {
sunoutputstream.write(buffer, 0, length);
}
sunoutputstream.flush();
sunoutputstream.close();
suninputstream.close();
Log.d("DataBase Copy :- ", "Copy Database Done");
}

public void replaceDataBase(String Path) {

// context.deleteDatabase(DATABASE_NAME);
FileInputStream suninputstream = null;
try {
suninputstream = new FileInputStream(new File(Path));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}

// InputStream suninputstream =new ByteArrayInputStream(new
// FileReader(file));

String sunFileName = DB_PATH + DATABASE_NAME;

OutputStream sunoutputstream;
try {
sunoutputstream = new FileOutputStream(sunFileName);
byte[] buffer = new byte[3000];

int length;

while ((length = suninputstream.read(buffer)) > 0) {
sunoutputstream.write(buffer, 0, length);
}
sunoutputstream.flush();
sunoutputstream.close();
suninputstream.close();
Log.d("DataBase Copy :- ", "Copy Database Done");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

public void openDataBase() throws SQLException {

String sunPath = DB_PATH + DATABASE_NAME;
dataBase = SQLiteDatabase.openDatabase(sunPath, null,
SQLiteDatabase.OPEN_READWRITE);
Log.d("DataBase Open :- ", "open Database Done");
}

@Override
public synchronized void close() {

if (dataBase != null)
dataBase.close();

super.close();
Log.d("DataBase close :- ", "close Database Done");
}

@Override
public void onCreate(SQLiteDatabase db) {
Log.d("DataBase Create :- ", "Create Database Done");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}





public ArrayList<byte[]> getBlobList() throws SQLException {
openDataBase();
ArrayList<byte[]> IDList = new ArrayList<byte[]>();
Cursor cursor = dataBase.rawQuery("select " + TABLE_IMAGES +" from "+ SQLiteHelper.TABLE_IMAGES, null);
cursor.moveToFirst();
if (cursor.getCount() > 0) {
do {
IDList.add(cursor.getBlob(0));
} while (cursor.moveToNext());
}
close();
return IDList;
}



}

No comments:

Post a Comment