图片未正确保存到数据库中(作为 BLOB)

发布于 2024-10-26 17:54:41 字数 1221 浏览 1 评论 0 原文

我尝试将图片作为 BLOB 保存到数据库中,但它没有正确存储在数据库中。

传递的 byte[] 是正确的,有 4300 个字符,而在数据库中它只有 12 个字符。 这就是数据库中存储的内容: [B@43e7be68

这是我的 SQL_INSERT 的代码:

public boolean SQL_INSERT_PICTURE(PictureDatatype type) {
    try{
        this.db = openHelper.getWritableDatabase();
        Log.d(TAG, "Insert picture");
        System.out.println("insert picture");
        db.execSQL("DELETE FROM picture " +
                "where " + LinkPic + " = '" + type.getLink() + "'");
        db.execSQL("INSERT OR REPLACE INTO picture " +
                "( " + NamePic + ", " + LinkPic + ", " + PicturePic + ") " +
                "VALUES ('" + type.getName() + "', '" + type.getLink() +"', '" + 
                type.getPicture() +"')");

        System.out.println("size of the picture (bytearray): " + type.getPicture().length);
        System.out.println("the picture: " + type.getPicture());
        System.out.println("insert complete");
        db.close();
        return true;
    }
        catch(SQLException s){
            System.out.println("Insert failed");
            db.close();
            return false;
        }
}

我的问题是为什么没有正确的 byte[] 存储在数据库中或者我该怎么做? 如果您需要更多信息或代码,请告诉我。

谢谢 柯比

I try to save a picture into the database as a BLOB, but its not stored properly in the database.

The byte[] which is passed is correct and has 4300 characters and in the database it just has 12 characters.
That's what is stored in the DB: [B@43e7be68

Here is the code of my SQL_INSERT:

public boolean SQL_INSERT_PICTURE(PictureDatatype type) {
    try{
        this.db = openHelper.getWritableDatabase();
        Log.d(TAG, "Insert picture");
        System.out.println("insert picture");
        db.execSQL("DELETE FROM picture " +
                "where " + LinkPic + " = '" + type.getLink() + "'");
        db.execSQL("INSERT OR REPLACE INTO picture " +
                "( " + NamePic + ", " + LinkPic + ", " + PicturePic + ") " +
                "VALUES ('" + type.getName() + "', '" + type.getLink() +"', '" + 
                type.getPicture() +"')");

        System.out.println("size of the picture (bytearray): " + type.getPicture().length);
        System.out.println("the picture: " + type.getPicture());
        System.out.println("insert complete");
        db.close();
        return true;
    }
        catch(SQLException s){
            System.out.println("Insert failed");
            db.close();
            return false;
        }
}

my question is why isn't the correct byte[] stored in the DB or how can i do it?
If u need more information or code pls tell me.

thx
kirbby

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天煞孤星 2024-11-02 17:54:41

使用此代码将图像转换为可以存储为 BLOB 的 ByteArray:

    private byte[] getBitmapAsByteArray(Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        // Middle value is quality, but PNG is lossless, so it's ignored.
        bitmap.compress(CompressFormat.PNG, 0, outputStream);
        return outputStream.toByteArray();
    }

OR

    private byte[] getBitmapAsByteArray(Bitmap bitmap) {
        final int width = bitmap.getWidth();
        final int height = bitmap.getHeight();
        ByteBuffer byteBuffer = ByteBuffer.allocate(width * height * 4);

        bitmap.copyPixelsToBuffer(byteBuffer);
        return byteBuffer.array();
    }

第一个选项应该始终有效。第二个选项假设每像素 32 位(ARGB_8888 格式),因此 4 个字节。如果您的位图是其他格式(例如565),则需要进行一些修改。

当您从数据库读取图像数据时,请执行以下操作:

   public Bitmap loadIcon(long iconId) {
       // Prepare the cursor to read from database....
       byte[] bitmapData = cursor.getBlob(cursor.getColumnIndex(Columns.Icons.DATA));

       return BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length);
   }

确保在执行“create table”SQL 语句时将数据库定义为 BLOB。

Use this code to convert the image to a ByteArray that can be stored as a BLOB:

    private byte[] getBitmapAsByteArray(Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        // Middle value is quality, but PNG is lossless, so it's ignored.
        bitmap.compress(CompressFormat.PNG, 0, outputStream);
        return outputStream.toByteArray();
    }

OR

    private byte[] getBitmapAsByteArray(Bitmap bitmap) {
        final int width = bitmap.getWidth();
        final int height = bitmap.getHeight();
        ByteBuffer byteBuffer = ByteBuffer.allocate(width * height * 4);

        bitmap.copyPixelsToBuffer(byteBuffer);
        return byteBuffer.array();
    }

First option should always work. Second option assumes 32bits-per-pixel (ARGB_8888 format) thus 4 bytes. if you're bitmap is of another format (e.g 565), it needs some modifications.

When you read the image data from the database, do it like this:

   public Bitmap loadIcon(long iconId) {
       // Prepare the cursor to read from database....
       byte[] bitmapData = cursor.getBlob(cursor.getColumnIndex(Columns.Icons.DATA));

       return BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length);
   }

Make sure that the the database is defined as BLOB when you execute your "create table" SQL statement.

挖个坑埋了你 2024-11-02 17:54:41

哎呀,看起来您正在尝试将 byte[] 数组传递到字符串中。

出于多种原因(安全性和性能),以这种方式组装 SQL 字符串是一个非常糟糕的主意。

请参阅这两个问题以了解准备好的语句,以及如何将值绑定到语句而无需连接 SQL:

使用java代码将图片插入数据库(sqlite)。我该怎么办?

Java异常错误 - Sqlite preparedStatement.setBlob

Ouch, that looks like you're trying to pass a byte[] array into a string.

Assembling your SQL strings in this way is a very bad idea, for multiple reasons (security and performance).

See these two questions for prepared statements, and how to bind values to the statement without having to concatenate your SQL:

insert a picture into database(sqlite) with java code. what should i do?

Java Exception Error - Sqlite preparedStatement.setBlob

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文