android开发之数据库存取图片.doc_第1页
android开发之数据库存取图片.doc_第2页
android开发之数据库存取图片.doc_第3页
android开发之数据库存取图片.doc_第4页
android开发之数据库存取图片.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

android开发之数据库存取图片Android数据库中存取图片通常使用两种方式,一种是保存图片所在路径,二是将图片以二进制的形式存储(sqlite3支持BLOB数据类型)。对于两种方法的使用,好像第二种方法不如第一种方法更受程序员欢迎,他们认为,在很多数据库语言里,处理大字段都是不容易的,像图片这样的文件放在数据库里会有问题:对数据库的读写速度永远赶不上文件系统的处理速度,使数据库变得巨大;但也有很多人认为像图片这样的数据存放在数据库中也有好处:易于备份,且备份速度绝对比备份文件快,比较容易数据迁移等等。其实这两种方法都有优缺点,具体使用哪种方法要视情况而定。个人倾向于使用数据库存取图片,因为个人认为存到数据库里的数据不会因外部数据的变化而丢失改变,比如你拍照获得一张图片,如果是将路径存到数据库,当这张照片被删除之后,下次读取数据库就得不到想要的结果了。接下来详细介绍数据库存取图片的方法:1.从资源中获取Bitmap对象1 Resources res = getResources();2 Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);2.把图片转换成字节byte/参数id为图片资源(R.drawable.img)1 public byte img(int id)2 3 ByteArrayOutputStream baos = new ByteArrayOutputStream();4 Bitmap bitmap = (BitmapDrawable) getResources().getDrawable(id).getBitmap();5 press(Bitmap.CompressFormat.PNG, 100, baos);6 return baos.toByteArray();7 3.在数据库中插入图片/在数据库创建时,图片字段的数据类型存储为 BLOB数据库插入操作public void onCreate(SQLiteDatabase db) String sql = create table + TB_NAME + ( + ID + integer primary key , + IMAGE + BLOB ) ; db.execSQL(sql); /将图片以字节形式存储数据库读取操作public long insert(byte img) SQLiteDatabase db = getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put(IMAGE, img); long result = db.insert(TB_NAME, null, cv); return result;4.获取存入数据库的图片(Bitmap)public Bitmap getBmp(int position) SQLiteDatabase db = getReadableDatabase(); Cursor cursor = db.select(TB_NAME); cursor.moveToPosition(position); byte in = cursor.getBlob(cursor.getColumnIndex(IMAGE); Bitmap bmpout = BitmapFactory.decodeByteArray(in, 0, in.length); return bmpout;/imgView.setImageBitmap(bm);5.转换获取的图片(Bitmap)为Drawable1 public Drawable chage_to_drawable(Bitmap bp)2 3 /因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。4 Bitmap bm=bp; 5 BitmapDrawable bd= new BitmapDrawable(getResource(), bm); 6 return bd;7 android 向数据库写入图片信息 读取图片信息向数据库写入图片信息:数据库中的字段设置为 binary类型Bitmap bitmap = BitmapFactory.decodeFile(path);ByteArrayOutputStream baos = new ByteArrayOutputStream(); press(CompressFormat.JPEG, 50, baos);String sql = insert into pic_info(pic_data, pic_name,pic_size,send_date,is_success) +values(?,?,?,?,?); Object args = new Objectbaos.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:URIJava代码import vider.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);sourceBpress(Bitmap.CompressFormat.JPEG, 50, outStream);outStream.close(); catch (Exception e) Log.e(TAG, “exception while writing image”, e); android创建数据库(SQLite)保存图片示例代码如下:/1.创建数据库public class DBService extends SQLiteOpenHelper private final static int VERSION = 1;private final static String DATABASE_NAME = uniteqlauncher.db;public DBService(Context context) this(context, DATABASE_NAME, null, VERSION);public DBService(Context context, String name, CursorFactory factory, int version) super(context, name, factory, version);Overridepublic void onCreate(SQLiteDatabase db) String sql = CREATE TABLE launcher( + _id INTEGER PRIMARY KEY AUTOINCREMENT, + photo BINARY); /保存为binary格式 db.execSQL(sql);Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) if(newVersion oldVersion) db.execSQL(DROP TABLE IF EXISTSlauncher); else return; onCreate(db);/保存图片到数据库public void savePhoto(Drawable appIcon, Context mContext)LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);View v = inflater.inflate(R.layout.app_view, null);ImageView iv = (ImageView) v.findViewById(R.id.appicon);iv.setImageDrawable(appIcon);String INSERT_SQL = INSERT INTO launcher(photo) values(?);SQLiteDatabase db = mDBService.getWritableDatabase(); / 得到数据库try ByteArrayOutputStream baos = new ByteArrayOutputStream();(BitmapDrawable) iv.getDrawable().getBitmap().compress(CompressFormat.PNG, 100, baos);/压缩为PNG格式,100表示跟原图大小一样Object args = new Object baos.toByteArray() ;db.execSQL(INSERT_SQL, args);baos.close();db.close(); catch (Exception e) e.printStackTrace();/3.从数据库中取图片public void getPhoto() String SELECT_SQL = SELECT photo FROM launcher;ImageView appIcon = (ImageView) v.findViewById(R.id.appicon);/v是我在类中定义的一个view对象,跟前面保存图片一样byte photo = null;mDBService = new DBService(getContext();SQLiteDatabase db = mDBService.getReadableDatabase();Cursor mCursor = db.rawQuery(SELECT_SQL, null);if (mCursor != null) if (mCursor.moveToFirst() /just need to query one timephoto = mCursor.getBlob(mCursor.getCo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论