使用 ORM Lite 从 SQLite 加载图像

Load image from SQLite using ORM Lite

我正在尝试检索保存在我的 SQLite 数据库中的图像。图片已正确保存,但当我尝试加载它时出现以下错误:

01-25 22:05:41.288  25041-25041/com.digitalnotebook E/CursorWindow﹕ Failed to read row 0, column 0 from a CursorWindow which has 0 rows, 6 columns.
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ 22:05:41.299 [main] ERROR c.d.adapters.CourseListAdapter - Can't read annotations (comments, photos, voices, etc) for id: 3
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.database.CursorWindow.nativeGetString(Native Method) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.database.CursorWindow.getString(CursorWindow.java:439) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at com.j256.ormlite.android.AndroidDatabaseResults.getString(AndroidDatabaseResults.java:134) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at com.j256.ormlite.field.types.DateStringType.resultToSqlArg(DateStringType.java:53) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at com.j256.ormlite.field.BaseFieldConverter.resultToJava(BaseFieldConverter.java:24) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at com.j256.ormlite.field.FieldType.resultToJava(FieldType.java:819) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at com.j256.ormlite.stmt.mapped.BaseMappedQuery.mapRow(BaseMappedQuery.java:60) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at com.j256.ormlite.stmt.SelectIterator.getCurrent(SelectIterator.java:270) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at com.j256.ormlite.stmt.SelectIterator.nextThrow(SelectIterator.java:161) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:202) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at com.j256.ormlite.dao.BaseDaoImpl.query(BaseDaoImpl.java:265) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at com.j256.ormlite.stmt.QueryBuilder.query(QueryBuilder.java:361) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at com.digitalnotebook.daos.PhotoDao.findLastPhotosByCourse(PhotoDao.java:32) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at com.digitalnotebook.managers.PhotoManager.findLastPhotosByCourse(PhotoManager.java:50) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at com.digitalnotebook.adapters.CourseListAdapter.onBindViewHolder(CourseListAdapter.java:94) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at com.digitalnotebook.adapters.CourseListAdapter.onBindViewHolder(CourseListAdapter.java:30) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:4138) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3448) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1810) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1306) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:523) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1988) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.View.layout(View.java:15745) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.ViewGroup.layout(ViewGroup.java:4868) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.View.layout(View.java:15745) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.ViewGroup.layout(ViewGroup.java:4868) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:543) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.View.layout(View.java:15745) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.ViewGroup.layout(ViewGroup.java:4868) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.widget.FrameLayout.onLayout(FrameLayout.java:388) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.View.layout(View.java:15745) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.ViewGroup.layout(ViewGroup.java:4868) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.widget.FrameLayout.onLayout(FrameLayout.java:388) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.View.layout(View.java:15745) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.ViewGroup.layout(ViewGroup.java:4868) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:890) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.View.layout(View.java:15745) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.ViewGroup.layout(ViewGroup.java:4868) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.widget.FrameLayout.onLayout(FrameLayout.java:388) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.View.layout(View.java:15745) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.ViewGroup.layout(ViewGroup.java:4868) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.View.layout(View.java:15745) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.ViewGroup.layout(ViewGroup.java:4868) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.widget.FrameLayout.onLayout(FrameLayout.java:388) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.View.layout(View.java:15745) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.ViewGroup.layout(ViewGroup.java:4868) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.widget.LinearLayout.onLayout(LinearLayout.java:1440) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.View.layout(View.java:15745) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.ViewGroup.layout(ViewGroup.java:4868) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.widget.FrameLayout.onLayout(FrameLayout.java:388) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.View.layout(View.java:15745) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.ViewGroup.layout(ViewGroup.java:4868) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2330) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2043) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6567) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.Choreographer.doCallbacks(Choreographer.java:603) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.Choreographer.doFrame(Choreographer.java:573) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.os.Handler.handleCallback(Handler.java:733) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.os.Handler.dispatchMessage(Handler.java:95) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.os.Looper.loop(Looper.java:136) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at android.app.ActivityThread.main(ActivityThread.java:5476) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at java.lang.reflect.Method.invokeNative(Native Method) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at java.lang.reflect.Method.invoke(Method.java:515) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) ~[na:0.0]
01-25 22:05:41.298  25041-25041/com.digitalnotebook I/System.out﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) ~[na:0.0]

密码是:

public List<Photo> findLastPhotosByCourse(Integer courseId) throws SQLException {
        List<Photo> photos = new ArrayList<Photo>();
        try {
            QueryBuilder<Photo, Integer> builder = getOrmliteDao().queryBuilder();
            builder.where()
                    .eq("id_lecture", courseId);
            builder.orderBy("date", false);
            builder.limit((4));
            photos = builder.query();
        } catch (SQLException e) {
            throw new SQLException();
        }
        return photos;
    }

并且在Photo.class中我定义了属性:

@DatabaseField(columnDefinition = "LONGBLOB not null", dataType = DataType.BYTE_ARRAY)
private byte[] image;

非常感谢!

忠告:永远不要将图像存储到数据库中。 这也给我们带来了解决方案:尝试将 link 保存到数据库中的图像,然后您可以使用它来调用图像。也许这会解决问题? 我希望它能回答你的问题,否则我希望别人能给你答案。

请检查图像文件的大小 sent/stored。您的代码理想情况下适用于较小的像素宽度 * 高度(间接较小的内存大小)

对我来说,它发生在行语句本身。

        cursor.moveToNext();

然后我意识到从服务器发送的图像存储太大了。相应地容纳所有对象大小。