mardi 27 octobre 2015

App with Java in AndroidStudio that uses SQLite database

I'm developing an app that takes pictures and save it with a text written by the user in the SQL database. For now I can make him take the picture and open the text box to the user types, but can not do it to save this text along with the image in the database, I wonder how I could do this. This is my code for now, divided into 2 parts:

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));
}

}

and this:

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

}

Aucun commentaire:

Enregistrer un commentaire