Skip to content Skip to sidebar Skip to footer

How To Store Image In Sqlite Database From Gallery

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 i dont

Solution 1:

Import form device by button click

Intentsdintent=newIntent(Intent.ACTION_PICK);
sdintent.setType("image/*");
startActivityForResult(sdintent, SD_REQUEST);

Get image form sd card

if (requestCode == SD_REQUEST) {
UriselectedImage= data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursorcursor= getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
cursor.moveToFirst();

intcolumnIndex= cursor.getColumnIndex(filePathColumn[0]);
StringfilePath= cursor.getString(columnIndex);
cursor.close();

BitmapyourSelectedImage= BitmapFactory.decodeFile(filePath);

testimage.setImageBitmap(yourSelectedImage);
ByteArrayOutputStreamstream=newByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
}

Save Image

DatabaseAdapterdbHelper=newDatabaseAdapter(Profiles.this);

       dbHelper.open();
       dbHelper.createUserProfiles( byteArray);
       dbHelper.close();

NOw in DatabaseAdapter.java

Define

publicstaticfinalStringU_PIC="picture";

Then

privatelongcreateUserTableContentValues(long id,byte[] byteImage) {
        ContentValuesvalues=newContentValues();
        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

Solution 2:

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.

Solution 3:

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)

privatebyte[] 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(); 
} 

Post a Comment for "How To Store Image In Sqlite Database From Gallery"