Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在访问之前,请确保光标已正确初始化_Android_Database_Sqlite_Cursor - Fatal编程技术网

Android 在访问之前,请确保光标已正确初始化

Android 在访问之前,请确保光标已正确初始化,android,database,sqlite,cursor,Android,Database,Sqlite,Cursor,我使用下一个代码来处理临时SQLite表: public class DBHelperForTemp extends SQLiteOpenHelper { private final static String DB_NAME = "tempdb"; private final static int DB_VERSION = 3; public DBHelperForTemp(Context context) { super(context, DB_NAME, null, DB_VE

我使用下一个代码来处理临时SQLite表:

public class DBHelperForTemp extends SQLiteOpenHelper {

private final static String DB_NAME = "tempdb";
private final static int DB_VERSION = 3;

public DBHelperForTemp(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE tbl ( ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, image BLOB )";
    db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS tbl");
    onCreate(db);
}

public boolean add_Temp_Values(String name, byte[] image) throws SQLiteException {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put("name", name);
    contentValues.put("image", image);

    long result = db.insert("tbl", null, contentValues);

    return result != -1;
}


public Cursor get_Data(){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT * FROM tbl";
    return db.rawQuery(query, null);
}


public void ClearData(){
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("delete from tbl");
}

}
将数据保存到临时文件:

private void  doTemp(){
    final Bitmap photo = ((BitmapDrawable) mPhotoImageView.getDrawable()).getBitmap();
    boolean insertData = mDBTemp.add_Temp_POI_Values(mNameEditText.getText().toString(),
           Utils.imageToByteArray(photo) );
    if (insertData){
        Toast.makeText(this, R.string.fb_success, Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, R.string.fb_error, Toast.LENGTH_SHORT).show();
    }
    finish();
}
并尝试从temp读取数据,并使用以下命令发送到firebase:

private void onSubmit(){
    final DBHelperForTemp mDBTemp = new DBHelperForTemp(this);
    final Cursor data = mDBTemp.get_Data();
    if (data != null && data.getCount() != 0 && data.moveToFirst()){
        for (int i = 0; i < data.getCount(); i++) {
            final String name = data.getString(data.getColumnIndex("name"));
            final byte[] byteArray = data.getBlob(data.getColumnIndex("image"));
            final Bitmap photo = BitmapFactory.decodeByteArray(byteArray, 0 , byteArray.length);
.........
有趣的是,若临时表中并没有图像,那个么代码就可以工作。如果有图像,应用程序会崩溃,但尝试读取名称字符串而不是图像

有人能帮我发现问题吗


getCount()返回1。data.getColumnIndex(“name”)返回0。data.getString(0)返回方法引发了“java.lang.IllegalStateException”异常。无法从游标窗口读取第0行第1列。在访问数据之前,请确保光标已正确初始化。

可能您存储的图像太大,光标窗口每行的最大大小为2048Kb

是。有8Mb。我可以从SQLite数据库中获取更大的图像吗?将它们保存在内部存储器中,而不是将数据库用于图像blob。你可以在这里找到样品
E/UncaughtException: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                                                                                         at android.database.CursorWindow.nativeGetString(Native Method)
                                                                                         at android.database.CursorWindow.getString(CursorWindow.java:451)
                                                                                         at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
                                                                                         at com.test.test.activities.ActivityMain.onSubmit