Buen día, estoy queriendo tomar una fotografía con la cámara y almacenarla en una base de datos sqlite en formato blob, pero al intentar guardar me tira el siguiente error.
android.database.sqlite.SQLiteException: incomplete input (code 1 SQLITE_ERROR): , while compiling: INSERT INTO registro VALUES (?,'36135737','combustible','864893033358675','0.0','0.0','00523522549@SANT@LUS ALDRO@M@36135737@B@09/02/1968@15/11/2017@206','869600046650575','[email protected]','220',null,'2019-10-09 10:57:02' at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
El código del activity es este (la verdad tome uno que insertaba en un archivo pero quiero adaptarlo a insertar en la base de datos local)
public class fotogasoil extends AppCompatActivity {
private static byte[] imagenComprimida;
private Button mboton;
private ImageView mPhotoImageView;
public static final int REQUEST_CODE_TAKE_PHOTO = 0 /*1*/;
private Uri photoURI;
private int requestCode;
private int resultCode;
private Intent data;
public static final String EXTRA_MESSAGE ="";
private CameraSource cameraSource;
private SurfaceView cameraViewQR;
private final int MY_PERMISSIONS_REQUEST_CAMERA = 1;
private String token = "";
private String tokenanterior = "";
public String textoCodigoQR ="";
public String DNI = "";
public String InfoDNI ="";
public String QR = "";
public String latitud ="";
public String longitud = "";
public String CMD = "combustible";
public String FECHAYHORA ="";
public String imeiCelular ;
public String email;
public EditText textoLitros ;
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gasoil_dialog);
// Views
mboton = findViewById(R.id.AceptarGasoil);
textoLitros = (EditText)findViewById(R.id.txtlitros);
mPhotoImageView = findViewById(R.id.imageViewgasoil);
Intent intent = getIntent();
textoCodigoQR = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
Log.e("leerQR anterior", ""+textoCodigoQR);
// // Listeners
mPhotoImageView.setOnClickListener(new View.OnClickListener() {
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) {
Log.e("entra", "ingresa despues del clic");
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_CODE_TAKE_PHOTO);
}
}
});
mboton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispatchTakePictureIntent();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_TAKE_PHOTO && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
assert extras != null;
Bitmap imageBitmap = (Bitmap) extras.get("data");
mPhotoImageView.setImageBitmap(imageBitmap);
getBytes(imageBitmap);
}
}
// convert from bitmap to byte array
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
imagenComprimida =stream.toByteArray();
Log.e("imagen", ""+imagenComprimida);
return stream.toByteArray();
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "MyPicture");
values.put(MediaStore.Images.Media.DESCRIPTION, "Photo taken on " + System.currentTimeMillis());
/* photoURI = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Uri photoURI = FileProvider.getUriForFile(fotogasoil.this, "com.example.android.fileprovider", photoFile);
*/
String ArrayDatos []= textoCodigoQR.split(",");
Log.e ("ArrayDatosv", ""+ArrayDatos);
DNI = ArrayDatos[0];
InfoDNI = ArrayDatos[1];
latitud =ArrayDatos[2];
longitud = ArrayDatos[3];
imeiCelular = ArrayDatos[4];
email = ArrayDatos[5];
QR=ArrayDatos[6];
InsertarDatos(CMD,DNI,QR, latitud, longitud, InfoDNI, email, imeiCelular,imagenComprimida);
Log.e("valor file", ""+photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
// startActivityForResult(takePictureIntent, REQUEST_CODE_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp;
File storageDir = getExternalFilesDir(Environment.DIRECTORY_DCIM);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
String mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
public void InsertarDatos (String CMD, String DNI,String QR, String latitud, String longitud, String InfoDNI, String email, String imeiCelular, byte[] imagenComprimida){
String CantidadLitros = textoLitros.getText().toString();
DateFormat df = new DateFormat();
df.format("yyyy-MM-dd hh:mm:ss a", new Date());
FECHAYHORA = (String) df.format("yyyy-MM-dd HH:mm:ss", new Date());
CreaBaseDeDatos dbHelper = new CreaBaseDeDatos(fotogasoil.this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.execSQL("INSERT INTO registro ('dni', 'cmd', 'imei','latitud','longitud','adicional','imeicelular','email','cantidadLitros','imagenComprimida','fechayhora') VALUES ('"+ this.DNI +"','"+ this.CMD +"','"+ this.QR +"','"+ this.latitud +"','"+ this.longitud +"','"+ InfoDNI +"','"+this.imeiCelular+"','"+this.email+"',"+CantidadLitros+",'"+imagenComprimida+"','"+FECHAYHORA+"'");
db.close();
}
}
Y esta es el activity donde creo la base de datos e intento insertar.
public class CreaBaseDeDatos extends SQLiteOpenHelper {
private static final int DB_VERSION = 2;
private static final String DB_NAME = "registros";
private static final String TABLE_Users = "registro";
private static final String KEY_ID = "id";
private static final String IMEI = "imei";
private static final String LATITUD = "latitud";
private static final String DNI = "dni";
private static final String LONGITUD = "longitud";
private static final String ADICIONAL = "adicional";
private static final String FECHAYHORA = "fechayhora";
private static final String CMD = "CMD";
private static final String LITROS = "CantidadLitros";
private static final String TICKET = "imagenComprimida";
private static final String EMAIL = "email";
private static final String IMEICELULAR = "imeicelular";
public static final String EXTRA_MESSAGE ="";
public String textoQR ="";
public CreaBaseDeDatos(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
String CREATE_TABLE = "CREATE TABLE " + TABLE_Users + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ DNI + " TEXT,"
+ CMD + " TEXT,"
+ IMEI + " TEXT,"
+ LATITUD + " TEXT,"
+ LONGITUD + " TEXT,"
+ ADICIONAL + " TEXT,"
+IMEICELULAR + " TEXT,"
+EMAIL + " TEXT,"
+ LITROS + " TEXT,"
+ TICKET + " BLOB,"
+ FECHAYHORA + " TEXT"+ ")";
db.execSQL(CREATE_TABLE);
Log.e("Base de datos", "entro a crear la base datos");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
// Drop older table if exist
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Users);
// Create tables again
onCreate(db);
}
}