0
I’m developing an app that takes photos and saves it along with a text written by the user himself in the SQL database. For now I can make him take the photo and open the text box for the user to type, but I can’t make him save this text along with the image in the database, I wonder how I could do it. That’s my code for now, divided into two 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));
}
}
are you getting any error? or just not saved? or saved and you can’t return the record?
– Marcelo Bonus
No error, it saves the image that was taken, however, when the text is written I cannot make it save in the table of the database.
– Ronaldo José