`

drawable

 
阅读更多

drawable 一些总结:

 

http://www.cnblogs.com/fighter/archive/2012/02/20/android-bitmap-drawable.html

(drawable的一些处理)

 

//1,已将图片保存到drawable目录下

//通过图片id获得Drawable

Resource res=gerResource();
Drawable drawable=res.getDrawable(id);//id为R.drawable.图片名称

//通过图片id获得Bitmap

Resource res=gerResource();

Bitmap bitmap=BitmapFactory.decodeResource(res, id);

//若只知道图片的名称,可以通过图片的名称获得图片的id

//name:图片的名称 defType:图片的类型(png,jpeg),defPackage:工程的包名
int id =int id =res.getIdentifier(name, defType, defPackage);
//获得id之后可以根据你的需要来获得Bitmap或Drawable

//2,已将图片保存到assest目录下

//知道图片的名称,通过inputstream打开图片

AssetManager asm=getAssetMg();
InputStream is=asm.open(name);//name:图片的名称
//获得Drawable
Drawable da = Drawable.createFromStream(is, null);
//获得Bitmap
Bitmap bitmap=BitmapFactory.decodeStream(is);

//3,图片保存在sdcard,已知图片的路径

//图片路径
String path = Environment.getExternalStorageDirectory().toString()+ "/DCIM/device.png";
RandomAccessFile mMiniThumbFile;
File imgfile = new File(path);
try {
    mMiniThumbFile = new RandomAccessFile(imgfile, "rw");
} catch (IOException ex) {
// Open as read-only so we can at least read the existing
// thumbnails.
try {
mMiniThumbFile = new RandomAccessFile(imgfile, "r");
} catch (IOException ex2) {
// ignore exception
System.out.println(ex2.toString());
}
}
data= new byte[10553];
try {
mMiniThumbFile.seek(0);
int got = mMiniThumbFile.read(data, 0, 10552);
System.out.println("got="+got);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.toString());
}
if (data != null) {
//通过data获得bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,data.length);

 

在进行Android开发过程中,我们经常会接触到Drawable对象(官方开发文档:A Drawable is a general abstraction for "something that can be drawn."),那么,若要使用数据库来进行存储及读取,该如何实现?
保存:

 

//第一步,将Drawable对象转化为Bitmap对象
Bitmap bmp = (((BitmapDrawable)tmp.image).getBitmap());
//第二步,声明并创建一个输出字节流对象
ByteArrayOutputStream os = new ByteArrayOutputStream();
//第三步,调用compress将Bitmap对象压缩为PNG格式,第二个参数为PNG图片质量,第三个参数为接收容器,即输出字节流os
bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
//第四步,将输出字节流转换为字节数组,并直接进行存储数据库操作,注意,所对应的列的数据类型应该是BLOB类型
ContentValues values = new ContentValues();
values.put("image", os.toByteArray());
db.insert("apps", null, values);
db.close();

 

代码看起来比较繁琐,是因为过程的确挺繁琐的,不过可以简单的总结为:
Drawable→Bitmap→ByteArrayOutputStream→SQLite

 

读取:

 

//第一步,从数据库中读取出相应数据,并保存在字节数组中
byte[] blob = cursor.getBlob(cursor.getColumnIndex("image"));
//第二步,调用BitmapFactory的解码方法decodeByteArray把字节数组转换为Bitmap对象
Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0, blob.length);
//第三步,调用BitmapDrawable构造函数生成一个BitmapDrawable对象,该对象继承Drawable对象,所以在需要处直接使用该对象即可
BitmapDrawable bd = new BitmapDrawable(bmp);

 很显然,读取是存储的相反过程,代码思路也差不多,但实现起来简单很多,总结思路为:
 

   SQLite→byte[]→Bitmap→BitmapDrawable


 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics