I'm developing an app that takes photos and saves them along with text written by the user in the SQL database. For now I can get it to take the picture and open the text box for the user to type, but I can not make it save that text along with the image in the database, I would like to know how I could do this. This is my code for now, divided into 2 parts:
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.ByteArrayOutputStream;
public class CameraToDatabase extends Activity {
private static final int CAMERA_REQUEST = 1888;
ImageView imageView1;
Imagehelper help;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_to_database);
help = new Imagehelper(this);
imageView1 = (ImageView) this.findViewById(R.id.imageView1);
Button B = (Button) this.findViewById(R.id.camera);
B.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
help.insert(byteArray);
}
}
public void getImage(View view) {
Cursor c = help.getAll();
if (c.moveToNext())
{
byte[] byteArray = c.getBlob(0);
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
imageView1.setImageBitmap(bmp);
}
c.close();
}
}
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by Davi on 02/08/2015.
*/
public class Imagehelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "clothes.db";
private static final int SCHEMA_VERSION = 3;
public Imagehelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE Image(_id INTEGER PRIMARY KEY AUTOINCREMENT,imageblob BLOB);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void insert(byte[] bytes) {
ContentValues cv = new ContentValues();
cv.put("imageblob", bytes);
Log.e("inserted", "inserted");
getWritableDatabase().insert("Image", "imageblob", cv);
}
public Cursor getAll() {
return (getReadableDatabase().rawQuery("SELECT imageblob FROM Image", null));
}
}