转换斑点 - >字节阵列 - >位图返回Java Android中的空位图

发布于 2025-01-18 17:07:51 字数 3093 浏览 3 评论 0原文

我将位图存储到我的SQL数据库中,通过将其转换为字节数组并将其作为blob存储在SQL表中。然后,后来我从字节数组中的表中检索了该斑点,然后将其转换回位图以在图像视图中显示。

为此,我首先将位图转换为字节阵列:

Bitmap thumbnail = ThumbnailUtils.extractThumbnail(currentBitmap, 512, 384);
byte[] thumbBytes = getBytesFromBitmap(thumbnail, 10);
myDB.createNewDoc(tempName, currentTable, thumbBytes, dateCreated);

这是我的getBytesFromBitMap()方法:

public byte[] getBytesFromBitmap(Bitmap bitmap, int quality) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.WEBP, quality, out);
        return out.toByteArray();
    }

currentBitMap是我的原始位图。

这是我用来创建我的表格的SQL查询,其中包含一个将存储位图的列:

sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS DOCS_DATA (SNO INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT UNIQUE NOT NULL, TABLE_NAME TEXT UNIQUE NOT NULL, THUMBNAIL BLOB, CREATEDON TEXT);");

此处的第二列是类型Blob的缩略图,其中包含转换为字节数组的位图。

为了将数据插入此表中,我正在使用以下查询:

public void createNewDoc(String doc_name, String table_name, byte[] thumbnail, String created_on) {
        sqLiteDatabase.execSQL("INSERT INTO DOCS_DATA (NAME, TABLE_NAME, THUMBNAIL, CREATEDON) VALUES ('" + doc_name + "', '" + table_name + "', '" + thumbnail + "', '" + created_on + "');");
    }

现在从表中检索数据,我正在使用以下方法:

public ArrayList<Object> getAllDocs() {
        ArrayList<String> names = new ArrayList<>();
        ArrayList<Integer> imagesNumber = new ArrayList<>();
        ArrayList<Bitmap> thumbnails = new ArrayList<>();
        ArrayList<String> createdon = new ArrayList<>();
        Cursor cur = sqLiteDatabase.rawQuery("SELECT NAME, TABLE_NAME, THUMBNAIL, CREATEDON FROM DOCS_DATA ORDER BY SNO;", null);
        for (int i = 0 ; i < cur.getCount() ; i++) {
            cur.moveToPosition(i);
            names.add(cur.getString(0));
            byte[] img = cur.getBlob(2);
            Log.e("BLOB ---- ", ""+img.toString());
            Log.e("BLOB ---- ", ""+img.length);
            Bitmap bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);
            Log.e("Bitmap", ""+bitmap);
            thumbnails.add(bitmap);
            createdon.add(cur.getString(3));
            Cursor cur1 = sqLiteDatabase.rawQuery("SELECT * FROM " + cur.getString(1) + ";", null);
            imagesNumber.add(cur1.getCount());
            cur1.close();
        }
        cur.close();
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(names);
        objects.add(imagesNumber);
        objects.add(thumbnails);
        objects.add(createdon);
        return objects;
    }

如您所见,我在代码和代码的最后一部分中有3个log.e(...)这就是我通过这3个日志所获得的:

E/BLOB ----: [B@ef5c8e2
E/BLOB ----: 29753
D/skia: --- Failed to create image decoder with message 'unimplemented'
E/Bitmap: null

在我在此处发布的代码的第一部分中,我将位图转换为字节数组。如果我立即将该字节阵列再次转换为位图,我不会变得无效。而且,我使用相同的代码将该字节数组转换为我在GetallDocs方法中使用的位图。

该空位图的原因可能是什么。任何帮助都将不胜感激。谢谢。

I am storing Bitmap into my sql database by converting it to byte array and storing it as BLOB in sql table. And then later I am retrieving that BLOB from the table in a byte array and then converting it back to bitmap to display it in an image view.

For that I first converted bitmap to byte array like this:

Bitmap thumbnail = ThumbnailUtils.extractThumbnail(currentBitmap, 512, 384);
byte[] thumbBytes = getBytesFromBitmap(thumbnail, 10);
myDB.createNewDoc(tempName, currentTable, thumbBytes, dateCreated);

This is my getBytesFromBitmap() method:

public byte[] getBytesFromBitmap(Bitmap bitmap, int quality) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.WEBP, quality, out);
        return out.toByteArray();
    }

Here currentBitmap is my original bitmap.

This is the sql query I am using to create my table that will contain a column in which bitmaps will be stored:

sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS DOCS_DATA (SNO INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT UNIQUE NOT NULL, TABLE_NAME TEXT UNIQUE NOT NULL, THUMBNAIL BLOB, CREATEDON TEXT);");

Here second last column is THUMBNAIL of type BLOB which contains bitmaps converted into byte array.

And to insert data into this table I am using the following query:

public void createNewDoc(String doc_name, String table_name, byte[] thumbnail, String created_on) {
        sqLiteDatabase.execSQL("INSERT INTO DOCS_DATA (NAME, TABLE_NAME, THUMBNAIL, CREATEDON) VALUES ('" + doc_name + "', '" + table_name + "', '" + thumbnail + "', '" + created_on + "');");
    }

Now to retrieve data from the table I am using the following method:

public ArrayList<Object> getAllDocs() {
        ArrayList<String> names = new ArrayList<>();
        ArrayList<Integer> imagesNumber = new ArrayList<>();
        ArrayList<Bitmap> thumbnails = new ArrayList<>();
        ArrayList<String> createdon = new ArrayList<>();
        Cursor cur = sqLiteDatabase.rawQuery("SELECT NAME, TABLE_NAME, THUMBNAIL, CREATEDON FROM DOCS_DATA ORDER BY SNO;", null);
        for (int i = 0 ; i < cur.getCount() ; i++) {
            cur.moveToPosition(i);
            names.add(cur.getString(0));
            byte[] img = cur.getBlob(2);
            Log.e("BLOB ---- ", ""+img.toString());
            Log.e("BLOB ---- ", ""+img.length);
            Bitmap bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);
            Log.e("Bitmap", ""+bitmap);
            thumbnails.add(bitmap);
            createdon.add(cur.getString(3));
            Cursor cur1 = sqLiteDatabase.rawQuery("SELECT * FROM " + cur.getString(1) + ";", null);
            imagesNumber.add(cur1.getCount());
            cur1.close();
        }
        cur.close();
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(names);
        objects.add(imagesNumber);
        objects.add(thumbnails);
        objects.add(createdon);
        return objects;
    }

As you can see I have 3 Log.e(...) in this last part of code and this is what I am getting through these 3 logs:

E/BLOB ----: [B@ef5c8e2
E/BLOB ----: 29753
D/skia: --- Failed to create image decoder with message 'unimplemented'
E/Bitmap: null

And in the very first part of code that I posted here, where I am converting Bitmap to byte array. If I convert that byte array immediately to bitmap again, I am not getting null. And I am using same code to convert that byte array to bitmap that I am using in my getAllDocs method.

What could be the cause of this null Bitmap. Any help would be much appreciated. Thank you.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文