Android 请求索引1,大小为3
我创建了一个简单的应用程序,其中图像存储从ImageView到数据库。 但是,当单击retrive按钮时,会显示索引1请求的大小为3。 我不知道出了什么问题。 数据库类:Android 请求索引1,大小为3,android,Android,我创建了一个简单的应用程序,其中图像存储从ImageView到数据库。 但是,当单击retrive按钮时,会显示索引1请求的大小为3。 我不知道出了什么问题。 数据库类: @Override public void onCreate(SQLiteDatabase db) { String CREATE_IMAGE_TABLE = "CREATE TABLE " +TABLE_NAME + "(" + IMAGE_KEY + " BL
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_IMAGE_TABLE = "CREATE TABLE " +TABLE_NAME + "("
+ IMAGE_KEY + " BLOB )";
db.execSQL(CREATE_IMAGE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(byte[]image )throws SQLiteException
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(IMAGE_KEY,image);
long result= db.insert(TABLE_NAME,null,cv);
if(result==-1)
return false;
else
return true;
}
public Cursor getAllData()
{
SQLiteDatabase db=this.getReadableDatabase();
Cursor res=db.rawQuery("select * from "+TABLE_NAME,null);
byte[]img=res.getBlob(0);
return res;
}
这是活动课:
public void button2(View view)
{
try {
Cursor res = myDb.getAllData();
if (res ==null) {
showMessage("error", "no data found");
} else {
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("id:" + res.getBlob(0) + "\n");
byte[] image = res.getBlob(0);
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0,
image.length);
imagee.setImageBitmap(bmp);
}
// showMessage("DATA", buffer.toString());
}
}
catch (Exception e)
{
Toast.makeText(getBaseContext(),e.getMessage(),
Toast.LENGTH_LONG).show();
}}
public void buttonn(View view)
{
Bitmap bitmap = ((BitmapDrawable) imagee.getDrawable()).getBitmap();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
byte[] data = outputStream.toByteArray();
boolean isInserted = myDb.insertData(data);
if (isInserted == true)
Toast.makeText(getBaseContext(), "Registration Succes!",
Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(), "No Record Registered!",
Toast.LENGTH_SHORT).show();
}
}
我尝试了大部分,但没有成功。我将其从res.movetoNext更改为,但显示相同的错误,并使用res.movetoFirst。它也显示相同的错误Android sqlite
CursorWindow
在大多数配置中有一个固定大小的2MB缓冲区。不能在大于该值的行中移动
不要在Android sqlite中存储大型二进制数据,如图像。改用外部存储,只需将路径保存在数据库中。检查以下有关如何在android中保存图像的代码:
private String saveToInternalStorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,"profile.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
fos.close();
}
return directory.getAbsolutePath();
}
您只需要将图像的路径添加到数据库中,而不是blob图像
参考资料:如何使用外部存储以及如何在Database中保存路径OK,但我使用了imagee.setImageResource(R.drawable.download);然后从ImageView中存储2或3 kb的图像,然后它也会显示相同的错误,原因可能是您已经在数据库中存储了大量数据,而查询部分在光标窗口大小限制方面存在问题。我的手机拍摄的图像约为3或4 MB。如何从ImageView向数据库添加图像路径并将其添加到检索。任何一个简单的例子,我可以得到。因为我是初学者的android