Android:存储在DB中的缩略图返回null

时间:2015-03-27 16:11:25

标签: android sqlite null

我有一个Db,我已经以ByteArray形式存储了视频及其缩略图的路径。但是当我尝试从数据库中检索缩略图时,我总是得到空值。这是我用来检索拇指和路径的代码:

        db=openOrCreateDatabase("VideoPaths", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS paths(path VARCHAR,thumbnail BLOB);");
        Cursor c = db.rawQuery("SELECT path,thumbnail FROM paths",null);
        int index = c.getColumnIndex("path");
        int index2 = c.getColumnIndex("thumbnail");

        c.moveToFirst();

            if (c != null) {
                // Loop through all Results
                do {

                    String VideoPath = c.getString(index);
                    byte[] bytes = c.getBlob(index2);
                    Bitmap thumb = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);


                    pathList.add(VideoPath);
                    listRow.add(new VideoRow(VideoPath,thumb, "User", "Testing"));

                } while (c.moveToNext());
            }

您对如何解决此问题有任何想法吗?

P.S视频路径检索工作正常。

编辑:这是填充表格的代码

Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath,
                            MediaStore.Images.Thumbnails.MINI_KIND);
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    thumb.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    byte[] thumbnailBitmapBytes = stream.toByteArray();

                    db=openOrCreateDatabase("VideoPaths", Context.MODE_PRIVATE, null);
                    db.execSQL("INSERT INTO paths (path,thumbnail) values('" + serverPath + "','"+thumbnailBitmapBytes+"')");

1 个答案:

答案 0 :(得分:1)

首先,我不会通过连接字符串来构造您的INSERT查询。请查看database.compileStatement(...) here。试着这样做:

SQLiteStatement stmt = db.compileStatement("INSERT INTO paths (path, thumbnail) values (?, ?)");
stmt.bindString(1, serverPath);
stmt.bindBlob(2, thumbnailBitmapBytes);
long newRowId = stmt.executeInsert();

然后,在花时间获取数据时,您可以使用已经共享过的代码。