在android上获取大数据到sqlite db

时间:2016-03-17 14:17:03

标签: java android sqlite

当我想要读取一张大图像(大约10MB)时,我收到以下错误:我之前保存到我的数据库中:

  

窗口已满:请求分配10052488字节,可用空间   2096638字节,窗口大小2097152字节

     

java.lang.IllegalStateException:无法读取第0行,第0列   CursorWindow

但是在保存图像时,不会发生错误。我使用以下代码行来获取值:

Erreur(11,12): PLS-00103: Encountered the symbol "(" when expecting one of the following: constant exception <identificateur> <identificateur entre guillemets> table long double ref char time timestamp interval date binary national character nchar The symbol "<identificateur>" was substituted for "(" to continue. Erreur(14,12): PLS-00103: Encountered the symbol "(" when expecting one of the following: constant exception <identificateur> <identificateur entre guillemets> table long double ref char time timestamp interval date binary national character nchar The symbol "<identificateur>" was substituted for "(" to continue. Erreur(17,12): PLS-00103: Encountered the symbol "(" when expecting one of the following: constant exception <identificateur> <identificateur entre guillemets> table long double ref char time timestamp interval date binary national character nchar The symbol "<identificateur>" was substituted for "(" to continue. Erreur(20,12): PLS-00103: Encountered the symbol "(" when expecting one of the following: constant exception <identificateur> <identificateur entre guillemets> table long double ref char time timestamp interval date binary national character nchar The symbol "<identificateur>" was substituted for "(" to continue. Erreur(23,12): PLS-00103: Encountered the symbol "(" when expecting one of the following: constant exception <identificateur> <identificateur entre guillemets> table long double ref char time timestamp interval date binary national character nchar The symbol "<identificateur>" was substituted for "(" to continue. Erreur(26,12): PLS-00103: Encountered the symbol "(" when expecting one of the following: constant exception <identificateur> <identificateur entre guillemets> table long double ref char time timestamp interval date binary national character nchar The symbol "<identificateur>" was substituted for "(" to continue. Erreur(29,12): PLS-00103: Encountered the symbol "(" when expecting one of the following: constant exception <identificateur> <identificateur entre guillemets> table long double ref char time timestamp interval date binary national character nchar The symbol "<identificateur>" was substituted for "(" to continue.

阅读this question让我相信除了不将图像保存到数据库之外没有其他解决方案。但是我的时间紧迫,改变这部分代码会花费我一段时间,所以如果有任何方法可以让它工作,我会非常感谢任何提示/黑客/解决方法。

相同的代码行适用于较小的图像(我只是尝试使用6MB图像而没有任何问题),所以我很确定我的Java代码中没有错误。此外,图像的保存过程完全没有问题。

2 个答案:

答案 0 :(得分:0)

我的应用程序必须将图像保存在SQlite DB中。

为了从DB读取大图像,我开发了以下代码。

int lengthCount = 2000000;

Cursor res = db.rawQuery("SELECT * FROM attributes WHERE length(image) < " + lengthCount + " and fk_feature = '" + idFeatures + "' AND cancel = 0 ORDER BY last_sync DESC", null);

        res.moveToFirst();
        while (!res.isAfterLast()) {
            //...
            // if length (image) <lengthCount no read errors occur
            //...
            res.moveToNext();
        }
        res.close();

Cursor r = db.rawQuery("SELECT id, length(image) FROM nameTable WHERE length(image) > " + lengthCount, null);
        r.moveToFirst();
        int startCount = 0;

        while (!r.isAfterLast()) {
            MyClassImage item = new MyClassImage();
            item.id = r.getString(r.getColumnIndex("id"));
            int lengthImage = r.getInt(r.getColumnIndex("length(image)"));
            while (lengthImage > startCount) {
                Cursor r1;
                if (lengthImage > (startCount + lengthCount)) {
                    r1 = db.rawQuery("SELECT substr(image, " + startCount + ", " + lengthCount + ") as x FROM attributes WHERE id ='" + item.id + "'", null);
                } else {
                    r1 = db.rawQuery("SELECT substr(image, " + startCount + ", " + (lengthImage - startCount) + ") as x FROM attributes WHERE id ='" + item.id + "'", null);
                }
                r1.moveToFirst();
                if (!r1.isAfterLast()) {
                    if (item.image != null && item.image.length > 0) {
                        item.image = ByteBuffer.allocate(item.image.length + r1.getBlob(r1.getColumnIndex("x")).length).put(item.image).put(r1.getBlob(r1.getColumnIndex("x"))).array();
                    } else {
                        item.image = r1.getBlob(r1.getColumnIndex("x"));
                    }
                }
                r1.close();
                startCount += lengthCount;
            }

注意:我希望找到一个更理想的解决方案,但目前可以正常工作。

答案 1 :(得分:-1)

使用光标后,请像cursor.close();一样将其关闭 它对我有用。