Reputation: 1709
in my app I have set an image from the gallery in an image view. Now I want to store that image in my SQL database.
Here is my SQL Database:
private static final String DATABASE_CREATE =
"create table " + DATABASE_TABLE + " ("
+ KEY_ROWID + " integer primary key autoincrement, "
+ KEY_TITLE + " text not null, "
+ KEY_BODY + " text not null, "
+ KEY_DATE_TIME + " text not null);";
Here I get the path of the image in the gallery and then set the image in the image view
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == PICK_FROM_FILE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Log.v("IMAGE PATH====>>>> ",selectedImagePath);
// Decode, scale and set the image.
Bitmap myBitmap = BitmapFactory.decodeFile(selectedImagePath);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, NEW_WIDTH, NEW_HEIGHT, true);
myBitmap.recycle();
myBitmap = null;
mImageView.setImageBitmap(scaledBitmap);
}
}
}
Upvotes: 2
Views: 4478
Reputation: 5390
Not a good idea.. You should save the image on the disk to reduce sql operations and to improve application performance.. Saving image in DB will make the database heavy in each sense.. Just store image on disk at desired location and save the path along with image name as string in database table.
Upvotes: 2
Reputation: 823
first create table like this db.execSQL("create table if not exists tablename(....,images BLOB,.....);");
then do your java code like this,
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Bitmap bit = BitmapFactory.decodeFile("your image path here");
bit.compress(CompressFormat.PNG, 0, outputStream);
SQLiteDatabase db = dbh.getWritableDatabase();
ContentValues cv = new ContentValues();
..
cv.put("images", outputStream.toByteArray());
..
..
db.insert("tablename", null, cv);
db.close();
this will store your image to db and,
byte []temp = c.getBlob(3);
Bitmap image = BitmapFactory.decodeByteArray(temp, 0, temp.length);
BitmapDrawable dd = new BitmapDrawable(image.getImage());
imgView.setBackgroundDrawable(dd);
this will retrive image from db..
Upvotes: 3
Reputation: 2571
OR, you save your image on the phone memory(on the SD Card), or Application cache etc.. and you store the path to retrieve your image file in your SQL database.
Upvotes: 2