android 向数据库写入图片信息 读取图片信息
2012-12-10 20:58
736 查看
向数据库写入图片信息:
数据库中的字段设置为 binary类型
Bitmap bitmap = BitmapFactory.decodeFile(path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 50, baos);
String sql = "insert into pic_info(pic_data, pic_name,pic_size,send_date,is_success) " +"values(?,?,?,?,?)";
Object[] args = new Object[]{baos.toByteArray(), name, size, now, isSucess};
db.insert(sql, args);
读取数据库的图片信息:
byte[] picData = cursor.getBlob(cursor.getColumnIndex("pic_data"));
bitmap.setImageBitmap(BitmapFactory.decodeByteArray(picData, 0, picData.length));
方法二:如果数据表入口是一个content:URI
Java代码
import android.provider.MediaStore.Images.Media;
import android.content.ContentValues;
import java.io.OutputStream;
// Save the name and description of an image in a ContentValues map.
ContentValues values = new ContentValues(3);
values.put(Media.DISPLAY_NAME, “road_trip_1″);
values.put(Media.DESCRIPTION, “Day 1, trip to Los Angeles”);
values.put(Media.MIME_TYPE, “image/jpeg”);
// Add a new record without the bitmap, but with the values just set.
// insert() returns the URI of the new record.
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
// Now get a handle to the file for that record, and save the data into it.
// Here, sourceBitmap is a Bitmap object representing the file to save to the database.
try {
OutputStream outStream = getContentResolver().openOutputStream(uri);
sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
outStream.close();
} catch (Exception e) {
Log.e(TAG, “exception while writing image”, e);
}
数据库中的字段设置为 binary类型
Bitmap bitmap = BitmapFactory.decodeFile(path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 50, baos);
String sql = "insert into pic_info(pic_data, pic_name,pic_size,send_date,is_success) " +"values(?,?,?,?,?)";
Object[] args = new Object[]{baos.toByteArray(), name, size, now, isSucess};
db.insert(sql, args);
读取数据库的图片信息:
byte[] picData = cursor.getBlob(cursor.getColumnIndex("pic_data"));
bitmap.setImageBitmap(BitmapFactory.decodeByteArray(picData, 0, picData.length));
方法二:如果数据表入口是一个content:URI
Java代码
import android.provider.MediaStore.Images.Media;
import android.content.ContentValues;
import java.io.OutputStream;
// Save the name and description of an image in a ContentValues map.
ContentValues values = new ContentValues(3);
values.put(Media.DISPLAY_NAME, “road_trip_1″);
values.put(Media.DESCRIPTION, “Day 1, trip to Los Angeles”);
values.put(Media.MIME_TYPE, “image/jpeg”);
// Add a new record without the bitmap, but with the values just set.
// insert() returns the URI of the new record.
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
// Now get a handle to the file for that record, and save the data into it.
// Here, sourceBitmap is a Bitmap object representing the file to save to the database.
try {
OutputStream outStream = getContentResolver().openOutputStream(uri);
sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
outStream.close();
} catch (Exception e) {
Log.e(TAG, “exception while writing image”, e);
}
相关文章推荐
- (一)上传图片至服务器,写入到数据库Blob字段中,以及从数据库读取Blob信息(iframe父子页面传值)
- 上传图片至服务器,写入到数据库Blob字段中,以及从数据库读取Blob信息(iframe父子页面传值)(1)
- (二)上传图片至服务器,写入到数据库Blob字段中,以及从数据库读取Blob信息(iframe父子页面传值)
- 上传图片至服务器,写入到数据库Blob字段中,以及从数据库读取Blob信息(iframe父子页面传值)(2)
- android写入数据库、读取sqlite中的图片
- Android 调用系统相机拍照(适配7.0)、从图库选择图片、从数据库读取联系人信息、相册图片、扫描文件
- 图片以BLOB存储在后台数据库中,Android客户端要进行读取显示
- 在数据库中进行图片的写入与读取
- 读取sql server 数据库图片信息
- 如何将位置信息写入JPEG图片文件【android】
- Android中通过Exifinterface读取图片地理位置信息
- android 读取,写入图片到sd卡源码
- android将图片转换存到数据库再从数据库读取转换成图片实现代码
- Android从文件目录中写入和读取图片
- android读取apk中已经存在的数据库信息
- Android ExifInterface 图片信息 读取时中文乱码解决方案
- 从数据库中读取图片文件和将图片和写入到数据库中
- Android 图片的读取与写入
- 保存图片信息到数据库,并可读取后显示在前端
- 直接读取MP3信息,专辑图片(非查系统数据库)