我已经解决了这个问题,但遇到另一个问题,select语句现在正在运行,但电子邮件意图没有运行(我没有收到任何电子邮件)
我的创建语句现在是;
myDB.execSQL("CREATE TABLE IF NOT EXISTS dressdetails " +
"(dress_id integer primary key AUTOINCREMENT, profile_id INTEGER, image1 BLOB, designer VARCHAR, style VARCHAR, size VARCHAR, viel VARCHAR, " +
"drycleaning VARCHAR, FOREIGN KEY(profile_id) REFERENCES profile(id));");
我有一个可点击的按钮,允许用户进入他们的设备库并检索照片
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
}
// Method to receive bundle with image Uri and convert to bitmap so it can be inputted into the database
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
try{
switch(requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = getContentResolver().openInputStream(selectedImage);
yourSelectedImage = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourSelectedImage.compress(Bitmap.CompressFormat.JPEG, 80, stream);
stream.toByteArray();
}
}
// Catch input/output errors
}catch(IOException ex){
ex.printStackTrace();
}
}
然后我尝试插入图像
myDB.execSQL("INSERT INTO dressdetails(profile_id, designer, style, size, viel, drycleaning) VALUES ('" + idReceived + "', '" +
yourSelectedImage + "', '" + Designer + "', '" + Style + "', '" + WhatSize + "', '" + WantViel + "', '" + DryCleaningCost + "');");
下一部分是我想错的地方;
/// Method to convert the image from int to byte[] so can be stored in database ///
public byte[] convertToByteArray(int image){
Resources resources = getResources();
Drawable drawable = resources.getDrawable(image);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
//byte[] bitmapData = stream.toByteArray();
return stream.toByteArray();
//return bitmapData;
}
/// Sample dresses inputted for your purposes, now when you search there will be some dresses to output /// + yourSelectedImage + "', '"
public void inputExamples() {
int image10 = R.drawable.wdone;
int image2 = R.drawable.wdtwo;
int image3 = R.drawable.wdthree;
int image4 = R.drawable.wdfour;
int image5 = R.drawable.wdfive;
int image6 = R.drawable.wdsix;
int image7 = R.drawable.wdseven;
int image8 = R.drawable.wdeight;
int image9 = R.drawable.wdnine;
byte[] eg1 = convertToByteArray(image10);
byte[] eg2 = convertToByteArray(image2);
byte[] eg3 = convertToByteArray(image3);
byte[] eg4 = convertToByteArray(image4);
byte[] eg5 = convertToByteArray(image5);
byte[] eg6 = convertToByteArray(image6);
byte[] eg7 = convertToByteArray(image7);
byte[] eg8 = convertToByteArray(image8);
byte[] eg9 = convertToByteArray(image9);
我直接准备好了我所看到的陈述,但如果我说实话,我只是对此不熟悉而且它有点过头了。
然后,我在这个连接表上有一个选择查询,以便将该连衣裙和其他我想要在电子邮件中转发的信息一起检索回来。
Cursor c = myDB.rawQuery("SELECT profile_id, image1, designer, drycleaning FROM dressdetails INNER JOIN profile ON (" +
"profile.id = dressdetails.profile_id) WHERE dressdetails.size LIKE '%" + size + "%' OR dressdetails.style LIKE '%" + style + "%' OR dressdetails.viel LIKE '%"
+ viel + "%';", null);*/
if(c.moveToFirst()){
do {
// store it as a string in a variable
dressesProfileId = c.getString(c.getColumnIndex("profile_id"));
/// *** HERE is where I store the image into a variable *** ///
//queryImageRetrieved = c.getString(c.getColumnIndex("image1"));
dryCleaningDetails = c.getString(c.getColumnIndex("drycleaning"));
} while (c.moveToNext());
}
c.close();
我将字符串转换为byte []
/// *** Here I try to convert the string back into a byte array *** ///
byte[] bytes;
try{
bytes = queryImageRetrieved.getBytes("UTF-8"); BitmapFactory.decodeByteArray(bytes, 0, queryImageRetrieved.length());
// Catch any I/O exceptions
System.out.println("You have reached here and bytes has a value " + bytes);
}catch (Exception e){
e.printStackTrace();
}
然后另一个光标检索匹配用户的联系信息
/// method 2 for returning contact details of matching dress from profile table and storing in variables ///
String rentorName = "";
String rentorEmail = "";
String rentalPrice = "";
String query = "SELECT username, useremail, rentalprice FROM profile INNER JOIN dressdetails ON (" +
"profile.id = dressdetails.profile_id) WHERE id = ?";
Cursor c1 = myDB.rawQuery(query, new String[] { dressesProfileId });
//Cursor c1 = myDB.rawQuery("SELECT username, useremail FROM profile INNER JOIN dressdetails ON (" +
//"profile.id = dressdetails.profile_id) WHERE profile.id = ?", new String[] {dressesProfileId});
if(c1.moveToFirst()) {
// Store the values needed in variables so we can send it to the user in an email with the image
do {
rentorName = c1.getString(c1.getColumnIndex("username"));
rentorEmail = c1.getString(c1.getColumnIndex("useremail"));
rentalPrice = c1.getString(c1.getColumnIndex("rentalprice"));
sellerContactDetails = "The name of the seller is " + rentorName + ", their email address is " + rentorEmail +
", the rental price is " + rentalPrice + " and the dry cleaning cost is " + dryCleaningDetails + ";";
// Check that it has worked - Debug tool
Toast.makeText(this, sellerContactDetails, Toast.LENGTH_SHORT).show();
// Continue until no more dresses match the set criteria
} while (c1.moveToNext());
}
// Close the cursor and the database
c1.close();
myDB.close();
最后,我的电子邮件意图是将检索到的信息和图像发送给用户。
// Send results to users email
Log.i("Send email", "");
String[] TO = { rentorEmail };
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("plain/text");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "P2P Weddings");
emailIntent.putExtra(Intent.EXTRA_TEXT, sellerContactDetails);
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email ", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(SearchCriteria.this,
"There is no email client installed.", Toast.LENGTH_SHORT).show();
}
我知道这是一个很长的抱歉,我已经接近完成我的第一个应用程序,但已经被困在这一点一个星期了!任何帮助都将受到大力赞赏。谢谢你们!
这就是logcat所说的
Caused by: android.database.sqlite.SQLiteException: unrecognized token: "[B@350f45fe, 'Versace', 'Off the shoulder', '6', 'Yes', '€25');" (code 1): , while compiling: INSERT INTO dressdetails(profile_id, image1, designer, style, size, viel, drycleaning) VALUES (1, [B@350f45fe, 'Versace', 'Off the shoulder', '6', 'Yes', '€25');
答案 0 :(得分:0)
yourSelectedImage是一个位图,在这里你将yourSelectedImage.toString()添加到表中:
myDB.execSQL("INSERT INTO dressdetails(profile_id, designer, style, size, viel, drycleaning) VALUES ('" + idReceived + "', '" +
yourSelectedImage + "', '" + Designer + "', '" + Style + "', '" + WhatSize + "', '" + WantViel + "', '" + DryCleaningCost + "');");
我建议不要将图像保存到数据库中,但是如果你这样做,你应该序列化Bitmap并确保你也正确地将其转义。
更新
根据您在问题中包含的错误消息,上述SQL语句等于此字符串:
"INSERT INTO dressdetails(profile_id, image1, designer, style, size,
viel, drycleaning) VALUES (1, [B@350f45fe, 'Versace', 'Off the shoulder',
'6', 'Yes', '€25');"
你看到“ [B @ 350f45fe ”?
这里显然有些错误,因为VALUES中的第二个参数应该用单引号括起来。我不知道它们是如何消失的,可能是一些溢出甚至是sql注入(或者即使它不是故意注入,如果yourSelectedImage.toString()有单引号,它也会呈现无效的SQL)。但无论如何,即使它工作,看起来你会保存Bitmap.toString,这就像'Bitmap [@ 350f45fe]',但不是实际的位图数据。