我使用下面的代码插入了捕获图像,并得到“not null”
,这表明数据不为空。
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if ((name != null && name.trim().length() > 0) && (result != null && result.trim().length() > 0)) {
// Toast.makeText(getActivity().getApplicationContext(), fk+"", Toast.LENGTH_LONG).show();
byte[] data=getBitmapAsByteArray(Global.img); // this is a function
if(data==null)
{
Toast.makeText(getActivity(), "null", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getActivity(), " not null", Toast.LENGTH_LONG).show();
SB.insertStaffBenefit(name, data, description, result, fk);
}
}
});
return claims;
}
public static byte[] getBitmapAsByteArray(Bitmap bitmap)
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
return outputStream.toByteArray();
}
但是,当我尝试检索捕获的图像时,应用程序崩溃了。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.receipt);
dbHelper = new MyDatabaseHelper(this);
final String k = getIntent().getExtras().getString("ID");
Toast.makeText(getApplicationContext(), k+"", Toast.LENGTH_LONG).show();
ImageView a=(ImageView)findViewById(R.id.imageView5);
database = dbHelper.getWritableDatabase();
c = database.rawQuery("SELECT s.Image FROM Information i LEFT JOIN StaffBenefit s ON s.Twd_id=i._id WHERE i._id=? ", new String[]{String.valueOf(k)},null);
if(c.moveToFirst())
{
byte[] img=c.getBlob(c.getColumnIndexOrThrow("Image"));
if(img!=null)
{
Log.e("TAG", " Not null");
}
else
{
Log.e("TAG", " null");
}
ByteArrayInputStream imageStream = new ByteArrayInputStream(img);
Bitmap theImage= BitmapFactory.decodeStream(imageStream);
a.setImageBitmap(theImage);
}
c.close();
}
}
MyDatabaseHelper.java
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_STAFF_BENEFIT + "( " + ID3 + " INTEGER PRIMARY KEY , Claim_Type TEXT, Image BLOB, Description TEXT , Amount TEXT, Twd_id INTEGER, FOREIGN KEY(Twd_id) REFERENCES "+TABLE_INFO+"(_id))");
}
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. at android.database.CursorWindow.nativeGetBlob(Native Method) at android.database.CursorWindow.getBlob(CursorWindow.java:404) at android.database.AbstractWindowedCursor.getBlob(AbstractWindowedCursor.java:45) at com.example.project.project.Receipt.onCreate(Receipt.java:40)
(Receipt.java:40)
byte[] img=c.getBlob(c.getColumnIndexOrThrow("Image"));
是否由大 Blob 引起的错误如 Retrieve large blob from Android sqlite database 中所述?我该如何解决它?谢谢
最佳答案
我认为您自己找到了解决方案。
你知道 onClick-Handler 中的一个/多个图像有多大吗? 如果它们大于 1Mb,您将无法将信息保存在一个游标中。请参阅您自己的解决方案链接。
但如果可能的话,也许您可以先测试一下。 尝试插入/选择大小小于 1Mb 的图像,然后插入/选择大小大于 1Mb 的图像。
如果第一个测试成功而第二个测试失败,您需要按照帖子中的解决方案进行操作:Retrieve large blob from Android sqlite database
关于java - 检索图像时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33627475/