检索图像时应用程序崩溃
App crashed when retrieve image out
我使用下面的代码插入了捕获图像并得到 "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"));
是否如Retrieve large blob from Android sqlite database所述由大斑点引起的错误?我该如何解决?谢谢
我想你自己找到了解决方案。
你知道onClick-Handler中的Image/Images is/are有多大吗?
如果它们大于 1Mb,您将无法在一个光标中保存信息。请参阅您自己的 link 以获取解决方案。
但也许您可以先测试一下,如果可能的话。
尝试 insert/select 一张小于 1Mb 的图片,然后一张大于 1Mb 的图片。
如果第一个测试成功而第二个没有,您需要按照 post 中的解决方案:Retrieve large blob from Android sqlite database
我使用下面的代码插入了捕获图像并得到 "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"));
是否如Retrieve large blob from Android sqlite database所述由大斑点引起的错误?我该如何解决?谢谢
我想你自己找到了解决方案。
你知道onClick-Handler中的Image/Images is/are有多大吗? 如果它们大于 1Mb,您将无法在一个光标中保存信息。请参阅您自己的 link 以获取解决方案。
但也许您可以先测试一下,如果可能的话。 尝试 insert/select 一张小于 1Mb 的图片,然后一张大于 1Mb 的图片。
如果第一个测试成功而第二个没有,您需要按照 post 中的解决方案:Retrieve large blob from Android sqlite database