存储和retreieve blob从sqllite数据库为Android [英] Store and retreieve blob from sqllite database for android

查看:252
本文介绍了存储和retreieve blob从sqllite数据库为Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道在eclipse中定义blob数据类型。我正在为Android开发一个应用程序,我想保存图像和数据库中的记录。我知道必须使用blob作为数据类型。我的问题是我如何在类和DBhandler类中定义它。有人可以为我提供代码帮助。

解决方案

Blob用于在SQLite中保存字节数组。



要将位图转换为字节数组,请使用以下代码:

  Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.thumbnail); 

ByteArrayOutputStream out = new ByteArrayOutputStream();

image.compress(Bitmap.CompressFormat.PNG,100,out);

byte [] buffer = out.toByteArray();要在blob类型中保存字节数组,请使用以下代码:




$ b

b $ b

  ContentValues cv = new ContentValues(); 
cv.put(CHUNK,buffer); // CHUNK blob type字段的表
long rawId = database.insert(TABLE,null,cv); // TABLE table name

有关链接


I need to know to define blob data type in eclipse. I am developing an app for android and i want to save image and a recording in database. I know have to use blob as the data type. My question is how do i define it in the class and the DBhandler class. Can someone provide me help with codes.

解决方案

Blob is used to save byte array in sqlite.

To convert a bitmap to byte array use following code:

Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.thumbnail);

ByteArrayOutputStream out = new ByteArrayOutputStream();

image.compress(Bitmap.CompressFormat.PNG, 100, out);

byte[] buffer=out.toByteArray();

To save a byte array in blob type use following code:

   ContentValues cv=new ContentValues();
   cv.put(CHUNK, buffer);       //CHUNK blob type field of your table
   long rawId=database.insert(TABLE, null, cv); //TABLE table name

Read more about Blob by this link

这篇关于存储和retreieve blob从sqllite数据库为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆