将Photo和Coordinates插入SQLite DB时出现StackOverflowError

时间:2012-09-04 16:10:03

标签: android sqlite stack-overflow

这是我第一次在Android环境中使用SQLite。我试图将照片和拍摄照片时用户的坐标插入SQLite数据库。

当我试图将照片插入数据库时​​,昨晚似乎工作正常(家用电脑),没有例外,虽然我找不到平板电脑创建数据库的位置,我尝试通过Eclipse data中的文件资源管理器查看DDMS文件夹,但是那里没有任何内容,使用Windows文件资源管理器Computer\SCH-I905\Tablet\Android\datacom.app等,但我的包裹不存在。

今天早上(工作计算机)我收到致命的StackOverflowError例外情况。我的代码有问题吗?我正在将图像转换为byte[],然后将其作为Blob存储到数据库,我使用byte[]而不是将byte[]转换为字符串,因为我读过一些建议反对StringBlob。任何帮助或建议都非常感谢,谢谢。

P.S。我的代码转换字符串toUpperCase()

中没有任何内容

背景

  • Google API 12
  • 在三星Galaxy 10.1 4G LTE平板电脑上进行测试
  • 我正在开发两台不同的计算机(家庭和工作),目前我将文件复制到一个拇指驱动器,然后将它们加载到我的工作区并启动Eclipse。由于签名冲突,我必须卸载应用程序,然后通过Eclipse重新运行应用程序。

在此我将图片转换为 byte[] ,然后发布到数据库:

public Camera.PictureCallback jpegHandler = new Camera.PictureCallback(){

    public void onPictureTaken(byte[] jpeg, Camera camera ){
        // TODO Work on the image insertion into the database

        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            image = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length);
            image.compress(Bitmap.CompressFormat.PNG, 100, bos);
            byte[] imageBlob = bos.toByteArray();

            UpdateGeoLocation updateGeo = new UpdateGeoLocation(getApplicationContext());

            double picLat = updateGeo.returnlocation.getLatitude();
            double picLng = updateGeo.returnlocation.getLongitude();

            Log.i("imageBlob", "" + imageBlob);
            Log.i("phoLat", "" + picLat);
            Log.i("phoLng", "" + picLng);

            dbControl.open();
            dbControl.addItem(imageBlob, picLat, picLng);
            dbControl.close();

            Log.i("InsertPhotoIntoDB", "WORKED!");
        } catch (SQLiteException e) {
            Log.e("InsertPhotoIntoDB", "FAILED: " + e.getMessage());
        }

以下是我的 DatabaseHelper.Java

的摘要
public class GlobalDBVars {
    public static final String DATABASE_NAME = "NST.db";
    public static final int DATABASE_VERSION = 2;
    public static final String TABLE_NAME = "photos_withCoordinates";

    public static final String KEY_ROWID = "_id";
    public static final String KEY_PHOTOS = "_photo";
    public static final String KEY_LAT = "_lat";
    public static final String KEY_LNG = "_lng";
}

private static final String DATABASE_CREATE = "CREATE TABLE IF NOT EXISTS " + 
    GlobalDBVars.TABLE_NAME + "(" + 
    GlobalDBVars.KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
    GlobalDBVars.KEY_PHOTOS + " BLOB NOT NULL, " + 
    GlobalDBVars.KEY_LAT + " REAL NOT NULL, " + 
    GlobalDBVars.KEY_LNG + " REAL NOT NULL);";

@Override
public void onCreate(SQLiteDatabase db) {
    try {
        db.execSQL(DATABASE_CREATE);
        onCreate(db);
        Log.i(TAG, "onCreate db: " + DATABASE_CREATE);
    } catch (SQLException e) {
        Log.e(TAG, "FAILED: " + e.getMessage());
    }
}

DatabaseControl.Java

中插入声明
public long addItem(byte[] photos, double _lat, double _lng) {
    ContentValues setUpVals = createContentValues(photos, _lat, _lng);

    return database.insert(GlobalDBVars.TABLE_NAME, null, setUpVals);
}

logcat的:

09-04 10:08:48.160: I/imageBlob(21529): [B@40751c18
09-04 10:08:48.160: I/phoLat(21529): 37.3674153
09-04 10:08:48.160: I/phoLng(21529): -77.4001403
09-04 10:08:48.240: I/dalvikvm(21529): threadid=1: stack overflow on call to Ljava/util/Locale;.getLanguage:L
09-04 10:08:48.240: I/dalvikvm(21529):   method requires 8+20+0=28 bytes, fp is 0x58f53318 (24 left)
09-04 10:08:48.240: I/dalvikvm(21529):   expanding stack end (0x58f53300 to 0x58f53000)
09-04 10:08:48.240: I/dalvikvm(21529): Shrank stack (to 0x58f53300, curFrame is 0x58f56d8c)
09-04 10:08:48.240: D/AndroidRuntime(21529): Shutting down VM
09-04 10:08:48.240: W/dalvikvm(21529): threadid=1: thread exiting with uncaught exception (group=0x40105760)
09-04 10:08:48.270: D/dalvikvm(21529): GC_CONCURRENT freed 95K, 8% free 8606K/9287K, paused 2ms+2ms
09-04 10:08:48.310: E/AndroidRuntime(21529): FATAL EXCEPTION: main
09-04 10:08:48.310: E/AndroidRuntime(21529): java.lang.StackOverflowError
09-04 10:08:48.310: E/AndroidRuntime(21529):    at java.lang.CaseMapper.toUpperCase(CaseMapper.java:143)
09-04 10:08:48.310: E/AndroidRuntime(21529):    at java.lang.String.toUpperCase(String.java:1619)
09-04 10:08:48.310: E/AndroidRuntime(21529):    at android.database.DatabaseUtils.getSqlStatementType(DatabaseUtils.java:1245)
09-04 10:08:48.310: E/AndroidRuntime(21529):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:100)
09-04 10:08:48.310: E/AndroidRuntime(21529):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:59)
09-04 10:08:48.310: E/AndroidRuntime(21529):    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1931)
09-04 10:08:48.310: E/AndroidRuntime(21529):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1864)
09-04 10:08:48.310: E/AndroidRuntime(21529):    at sompackage.nst.DatabaseHelper.onCreate(DatabaseHelper.java:49)

2 个答案:

答案 0 :(得分:1)

你正在递归地呼叫onCreate()

@Override
public void onCreate(SQLiteDatabase db) {
    try {
        db.execSQL(DATABASE_CREATE);
        **onCreate(db);**
...
}

删除onCreate(db)电话

答案 1 :(得分:-1)

旋转调用onCreate(db)。

@Override
public void onCreate(SQLiteDatabase db) {
try {
    onCreate(db);
}