I'm a newbie in Android,
I just wondering how to store an image to SQLite database in Android, well i have database look like this.
Photo
id (int) | image (BLOB)
Then i have a class to get an image from gallery..
LogoSQLiDemoActivity
public class LogoSQLiteDemoActivity extends Activity implements OnClickListener{
ContactImageAdapter imageAdapter;
Validation valid;
DBDataSource db;
private ArrayList<image> imageArry = new ArrayList<image>();
public static final int MEDIA_TYPE_IMAGE = 1;
private static final int CAMERA_REQUEST = 1;
private static final int PICK_FROM_GALLERY = 2;
int imageId;
byte[] imageName;
String nama_foto;
String nama;
Bitmap theImage;
byte imageInByte[];
private Long id;
//widget
private EditText edNama_foto;
Button addImage;
Button cancel;
ListView dataList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_data_photo2);
/*** create DatabaseHandler object*/
db = new DBDataSource(this);
//error in here
db.open();
dataList = (ListView) findViewById(R.id.list);
cancel = (Button)findViewById(R.id.btnCancel);
addImage = (Button) findViewById(R.id.btnAdd);
cancel.setOnClickListener(this);
Sma sekolah = db.getLastSma();
id = sekolah.getId();
/**
* Reading and getting all records from database
*/
List<image> img = db.getAllImage_Logo(id);
for (image cn : img)
{
// add contacts data in arrayList
imageArry.add(cn);
/** Set Data base Item into listview}*/
imageAdapter = new ContactImageAdapter(this, R.layout.screen_list, imageArry);
dataList.setAdapter(imageAdapter);
}
/**
* go to next activity for detail image
*/
dataList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
final int position, long id) {
imageName = imageArry.get(position).getLokasi_foto();
imageId = imageArry.get(position).get_id_sma();
Log.d("Before Send:****", imageName + "-" + imageId);
// convert byte to bitmap
ByteArrayInputStream imageStream = new ByteArrayInputStream(
imageName);
theImage = BitmapFactory.decodeStream(imageStream);
Intent intent = new Intent(LogoSQLiteDemoActivity.this,
DisplayImageActivity2.class);
intent.putExtra("imagename", theImage);
intent.putExtra("imageid", imageId);
startActivity(intent);
}
});
/**
* open dialog for choose camera/gallery
*/
final String[] option = new String[] { "Ambil dari Kamera",
"Pilih dari Album" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.select_dialog_item, option);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pilihan");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.e("Pilihan", String.valueOf(which));
if (which == 0) {
callCamera();
}
if (which == 1) {
callGallery();
}
}
});
final AlertDialog dialog = builder.create();
addImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.show();
}
});
}
/**
* On activity result
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case CAMERA_REQUEST:
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap yourImage = extras.getParcelable("data");
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte imageInByte[] = stream.toByteArray();
Log.e("output before conversion", imageInByte.toString());
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addImage3(new image(imageInByte));
Intent i = new Intent(LogoSQLiteDemoActivity.this, LogoSQLiteDemoActivity.class);
startActivity(i);
finish();
}
break;
case PICK_FROM_GALLERY:
Bundle extras2 = data.getExtras();
if (extras2 != null)
{
Bitmap yourImage = extras2.getParcelable("data");
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte imageInByte[] = stream.toByteArray();
Log.e("output before conversion", imageInByte.toString());
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addImage3(new image(imageInByte));
Intent i = new Intent(LogoSQLiteDemoActivity.this,
LogoSQLiteDemoActivity.class);
startActivity(i);
finish();
}
}
}
/**
* open camera method
*/
public void callCamera() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("crop", "true");
cameraIntent.putExtra("aspectX", 0);
cameraIntent.putExtra("aspectY", 0);
cameraIntent.putExtra("outputX", 150);
cameraIntent.putExtra("outputY", 150);
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
/**
* open gallery method
*/
public void callGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(
Intent.createChooser(intent, "Complete action using"),
PICK_FROM_GALLERY);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnCancel:
Intent intent = new Intent(getApplicationContext(), MenuAdmin.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
}
and this a method i use to store picture in sqlite
public void addImage3(image img) {
open(); //it's mean to open the connection from database
//THE Question is right here, how i can put a byte array into database
//without using this method, a.k.a INSERT INTO, cause i have tried to search any
//solution in google, but i can't solve my problem
ContentValues values = new ContentValues();
values.put(image, img._lokasi_foto);
// Inserting Row
database.insert(Photo, null, values);
close(); // Closing database connection
}
and here's my image class
public class image {
public byte[] _lokasi_foto;
//this is getter
public byte[] getLokasi_foto() {
return _lokasi_foto;
}
//this is setter
public void setLokasi_foto(byte[] lokasi_foto) {
this._lokasi_foto = lokasi_foto;
}
and this the constructor
public image(byte[] lokasi_foto) {
this._lokasi_foto = lokasi_foto;
}
}
Can someone help me with this problem, cause i have been search in google, but i can't still solve my problem, Please Help...