简体   繁体   English

SQLite数据库Android应用程序在模拟器中无法在我的设备中正常运行

[英]SQLite database Android app works fine in emulator not in my device

I am trying to create local image database using SQLite. 我正在尝试使用SQLite创建本地图像数据库。 The app works well in the AVD, but when I try to run in my Samsung S6(Android 7.0, API 24). 该应用程序在AVD中运行良好,但是当我尝试在Samsung S6(Android 7.0,API 24)中运行时。 After several attempts of failures in running the app I have restore my device. 经过几次尝试运行失败的应用程序,我已经恢复了我的设备。 Then app worked once or twice then it started showing the same errors as given below. 然后应用程序运行了一次或两次,然后开始显示以下相同的错误。

    06-12 19:34:10.292 17670-22492/com.example.eberhardt.test16 E/CursorWindow: Failed to read row 0, column 1 from a CursorWindow which has 0 rows, 2 columns.
06-12 19:34:10.343 17670-22492/com.example.eberhardt.test16 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
    Process: com.example.eberhardt.test16, PID: 17670
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:318)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:762)
     Caused by: 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.nativeGetBlob(Native Method)
        at android.database.CursorWindow.getBlob(CursorWindow.java:416)
        at android.database.AbstractWindowedCursor.getBlob(AbstractWindowedCursor.java:45)
        at com.example.eberhardt.test16.MainActivity$featureDetection.doInBackground(MainActivity.java:343)
        at com.example.eberhardt.test16.MainActivity$featureDetection.doInBackground(MainActivity.java:314)
        at android.os.AsyncTask$2.call(AsyncTask.java:304)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
        at java.lang.Thread.run(Thread.java:762)

Codes for creating the image database is 用于创建图像数据库的代码是

package com.example.eberhardt.test16;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import static android.provider.BaseColumns._ID;

public class imageDatabase extends SQLiteOpenHelper {
    private static final String TAG = imageDatabase.class.getSimpleName();

    private static final String DATABASE_NAME = "features.db";
    private static final int DATABASE_VERSION = 1;
    ContentResolver mContentResolver;

    public final static String COLUMN_NAME = "imagename";
    public final static String TABLE_NAME = "imagetable";

    public imageDatabase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        final String SQT_CREATE_IMAGE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_NAME + " BLOB" + ");";
        db.execSQL(SQT_CREATE_IMAGE_TABLE);
        Log.e(TAG, "Database creates successfully");
    }

    public void addToDb(byte[] image) throws SQLException {
        ContentValues cv = new ContentValues();
        cv.put(COLUMN_NAME, image);
        SQLiteDatabase db = this.getWritableDatabase();
        db.insert(TABLE_NAME, null, cv);
        db.close();
    }

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

To store the Images 存储图像

switch (requestCode){
            case OPEN_GALLERY:
                if(resultCode == RESULT_OK && data != null){
                    Uri uri = data.getData();
                    try {
                        imageBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                    imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
                    byte[] uploadData = outputStream.toByteArray();

                    imageDatabase dataBase = new imageDatabase(this);
                    dataBase.addToDb(uploadData);
                }
                break;
        }

To read the images 读取图像

protected Void doInBackground(Void... voids) {
            imageDatabase imageDatabase = new imageDatabase(MainActivity.this);
            SQLiteDatabase mDatabase = imageDatabase.getReadableDatabase();
            Cursor cursor = mDatabase.rawQuery("Select * FROM imagetable", null);

            cursor.moveToFirst();
            Log.e("Number of rows", String.valueOf(cursor.getCount()));
            for(int i = 1; i <= cursor.getCount(); i++) {
                foundImage = false;
                byte[] imageReceive = cursor.getBlob(1);

                imageRDBitmap = BitmapFactory.decodeByteArray(imageReceive, 0, imageReceive.length);
                Mat imageConverted = new Mat(imageRDBitmap.getHeight(), imageRDBitmap.getWidth(), CvType.CV_8UC4);
                Utils.bitmapToMat(imageRDBitmap, imageConverted);

                initializeFeatures(imageConverted);
                int matchedPoints = recognizeFeature(ImageFrame);
                //Log.e("Checking for matched", String.valueOf(matchedPoints));
                if (matchedPoints > 20){
                    columnID = cursor.getInt(0);
                    foundImage = true;
                    break;
                }else{
                    cursor.moveToNext();
                }


            }
            if(cursor != null && !cursor.isClosed()){
                cursor.close();
                Log.e("RECEIVED FROM DATABASE", "is empty");
            }
            return null;
        }

The above piece of code is to store and retrieve the images. 上面的代码是存储和检索图像。 Also in the Android manifest, I have given permission to READ and WRITE. 同样在Android清单中,我已授予读取和写入权限。 Kindly help to find the solution for this problem. 请帮助找到该问题的解决方案。

Thank you. 谢谢。

The main line in the error log we need to understand is: 我们需要了解的错误日志的主线是:

Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

So the Cursor is opened here: 因此,光标在此处打开:

Cursor cursor = mDatabase.rawQuery("Select * FROM imagetable", null);

Looks like it is not getting anything back from that query. 看起来它没有从该查询中得到任何回报。 I would check your database on your devices to make sure that there is a row in the table. 我会检查您设备上的数据库,以确保表中有一行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的应用在模拟器上运行正常,但在Android设备上却无法正常运行 - My App is working fine on Emulator but not on android device Android设备上的SQLite异常:没有这样的表,但是模拟器可以正常工作吗? - SQLite exception on Android Device : No such table, but emulator works fine? 我的JavaMail应用程序可在模拟器上正常运行,但无法在我的Android移动设备上运行 - My JavaMail app works fine on Emulator but it is not working on my android mobile device SQLite数据库可在模拟器上运行,但不能在设备上运行 - SQLite database works on emulator but not on device 带sqlite的PhoneGap(3.4.0)android应用,在模拟器中工作正常,但在设备中工作不正常 - PhoneGap(3.4.0) android app with sqlite, its working fine in emulator but not in device 为什么我的 android 应用程序在模拟器中运行良好,但在真实设备中显示非常糟糕/ - why my android app works fine in emulator but display is totally terrible in real device/ Android应用程式可在模拟器上正常运作,但在真实装置中当机 - Android app works fine in emulator but crashes in real device Android应用在模拟器上工作正常,但在设备上无效。 使用Internet连接 - Android app works fine on Emulator, but not on Device. Uses Internet Connection 数据库在模拟器中工作正常,但在设备上出错 - Database works fine in emulator, but gives an error on device 应用程序可以在模拟器上正常运行,但不能在移动设备上运行 - App works fine on Emulator but not on Mobile device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM