Android使用sqlite数据库创建book表,book表有字段image varchar(80)怎么把drawable里面的图片在创建表后insert进表里面
时间: 2024-03-23 13:40:13 浏览: 21
要将drawable里的图片插入到SQLite数据库表中,可以先将图片转换为字节数组,然后将字节数组插入到表中。以下是一个示例代码:
```java
// 获取drawable里的图片
Drawable drawable = getResources().getDrawable(R.drawable.my_image);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
// 将图片转换为字节数组
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imageBytes = stream.toByteArray();
// 创建数据库并插入数据
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("image", imageBytes);
db.insert("book", null, values);
```
在上面的代码中,`getResources().getDrawable(R.drawable.my_image)` 可以获取drawable里的图片,然后使用 `Bitmap` 将其转换为字节数组。接着,创建一个 `ContentValues` 对象,将字节数组插入到 `image` 字段中。最后调用 `insert` 方法将数据插入到 `book` 表中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)