【问题标题】:Can't insert Image byte array into SQLite Android无法将图像字节数组插入 SQLite Android
【发布时间】:2020-12-18 11:09:12
【问题描述】:

我正在尝试从图库中获取图像并首先从 URI 将其转换为位图,然后将其转换为字节数组,然后尝试将其存储到 BLOB 数据类型列的数据库中。插入查询返回成功消息但不反映到数据库中。但是当我将空字节数组传递给它时,它会存储整行。

Bitmap 到 byte[] 的代码:

byte[] imgByteArray;

        int size = bitmap.getRowBytes() * bitmap.getHeight();
        ByteBuffer byteBuffer = ByteBuffer.allocate(size);
        bitmap.copyPixelsToBuffer(byteBuffer);
        imgByteArray = byteBuffer.array();

更新:

从图库和字节转换中获取图像并存储到数据库中的代码:

Intent intent = new Intent();
                intent.setType("image/*");
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_PICTURE);


public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {

            

            if (requestCode == PICK_PICTURE) {
                if (data.getData() != null) {
                    Uri uri = data.getData();

                    try {
                        pictureBitmap = getBitmapFromUri(uri);

            byte[] imgByteArray;

                int size = pictureBitmap.getRowBytes() * pictureBitmap.getHeight();
                ByteBuffer byteBuffer = ByteBuffer.allocate(size);
                pictureBitmap.copyPixelsToBuffer(byteBuffer);
                imgByteArray = byteBuffer.array();

            


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    }
   }
}

    public boolean insertPicture(byte[] picture, String desc){

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues cValues = new ContentValues();

        cValues.put(KEY_PICTURE, picture);
        cValues.put(KEY_DESC, desc);
        
        long rowId = db.insert(PICTURE_TABLE,null, cValues);
        Log.d("TAG","Row ID : "+rowId);

        db.close();
        if(rowId!=0){
            return true;
        }else {
            return false;
        }

    }

【问题讨论】:

  • 请提供您用于从图库中获取图像、将此图像存储到数据库以及从数据库中检索它的代码
  • 我已经用代码更新了这个问题。请看一下。谢谢
  • trying to get image from gallery and convert it into Bitmap first 这看起来是个坏主意。特别是因为您想在保存之前将其所有像素放入一个数组中。这将花费您大量的存储空间。与您刚刚存储所选 jpg 文件时相比,可能要多 20 倍。
  • @blackapps 哦,我不知道这个。那么从您的角度来看,为了更好地管理内存,实现这一目标的最佳方法是什么?
  • 我已经告诉过你了:存储所选 jpg 文件本身的字节数。不要使用中间位图。

标签: android android-studio android-sqlite


【解决方案1】:

在 onActivityResult 方法中:

Uri uri = data.getData();
InputStream inStream = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inStream);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, outStream);       
byte[] imgBytes = outStream.toByteArray();
SQLiteDatabase db = dbHelper.getWritableDatabase();
String sql = "INSERT INTO YOURTABLENAME (id,image) VALUES(?,?)";
SQLiteStatement statement = db.compileStatement(sql);
statement.clearBindings();
statement.bindString(1, Integer.toString(someId));
statement.bindBlob(2, imgBytes);
statement.executeInsert();
db.close();

我建议您使用 id 存储图像字节以便轻松检索它

【讨论】:

  • 您正在将 jpg 图像文件转换为 png 图像。 Png需要更多内存。通常人们希望使用尽可能少的内存。
  • 你是对的,但我认为问题是关于如何做到这一点,而不是如何做到最好)
  • @alex-rmcf 所以你的意思是,我必须这样设置图像?不能使用我现在使用的方法?我的意思是 db.INSERT 方法?
  • 所以如果我想这样使用:SQLiteDatabase db = this.getWritableDatabase();内容值 cValues = new ContentValues(); cValues.put(KEY_PICTURE, 图片); cValues.put(KEY_DESC, desc); long rowId = db.insert(PICTURE_TABLE,null, cValues); Log.d("TAG","行 ID : "+rowId); db.close(); if(rowId!=0){ 返回真; }else { 返回假; } 我不能用吗?
  • 并根据性能 - using 语句比内容值更快。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-15
  • 2012-10-18
  • 2019-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多