i have tried this code to upload an image from gallery to sqllite database in my application but when my application tries to open gallery it gives FORCE TO CLOSE Error and
Using BLOB data type we can store an image in the SQLite database. The data that actually gets stored in the database is the bytes that make up an image or a file.
As for obtaining pic from gallery, look here, it seems, you have forgotten "super" call.
db.execSQL("CREATE TABLE images ("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "data BLOB,"
+ "hash BLOB UNIQUE"
+ ");");
To convert the image to a BLOB, use the following code: (Note that we're compressing the image here)
private byte[] getBitmapAsByteArray(Bitmap bitmap)
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// Middle parameter is quality, but since PNG is lossless, it doesn't matter
bitmap.compress(CompressFormat.PNG, 0, outputStream);
return outputStream.toByteArray();
}
Import form device by button click
Intent sdintent = new Intent(Intent.ACTION_PICK);
sdintent.setType("image/*");
startActivityForResult(sdintent, SD_REQUEST);
Get image form sd card
if (requestCode == SD_REQUEST) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
testimage.setImageBitmap(yourSelectedImage);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
}
Save Image
DatabaseAdapter dbHelper = new DatabaseAdapter(Profiles.this);
dbHelper.open();
dbHelper.createUserProfiles( byteArray);
dbHelper.close();
NOw in DatabaseAdapter.java
Define
public static final String U_PIC = "picture";
Then
private long createUserTableContentValues(long id,byte[] byteImage) {
ContentValues values = new ContentValues();
values.put(ID, id);
values.put(U_PIC, byteImage);
return database.insert(IMAGE_INSERT, null, values);
}
I think this might be help you.....
Other resources: http://www.helloandroid.com/tutorials/store-imagesfiles-database
http://www.coderanch.com/t/507054/Android/Mobile/Storing-image-database
http://hi.baidu.com/_java/blog/item/971e142a13afe6305243c12f.html
http://www.tutorialforandroid.com/2009/10/how-to-insert-image-data-to-sqlite.html