‘壹’ android如何从SD卡读取图片文件转化为bitmap
SDK 中有专门取SD卡路径的静态方法
public String getSDPath(){
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState()
.equals(Android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();//获取跟目录
}
return sdDir.toString();
}
不要写死路径
‘贰’ android 已经知道路径怎么将路径中的图片变成Bitmap
/**
* 获取本地图片并指定高度和宽度
*/
public static Bitmap getNativeImage(String imagePath)
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 获取这个图片的宽和高
Bitmap myBitmap = BitmapFactory.decodeFile(imagePath, options); //此时返回myBitmap为空
//计算缩放比
int be = (int)(options.outHeight / (float)200);
int ys = options.outHeight % 200;//求余数
float fe = ys / (float)200;
if (fe >= 0.5)
be = be + 1;
if (be <= 0)
be = 1;
options.inSampleSize = be;
//重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false
options.inJustDecodeBounds = false;
myBitmap = BitmapFactory.decodeFile(imagePath, options);
return myBitmap;
}
/**
* 以最省内存的方式读取本地资源的图片 或者SDCard中的图片
* @param imagePath
* 图片在SDCard中的路径
* @return
*/
public static Bitmap getSDCardImg(String imagePath)
{
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
//获取资源图片
return BitmapFactory.decodeFile(imagePath, opt);
}
‘叁’ android中Bitmap存为一张图片
可以用Bitmap.compress函数来把Bitmap对象保存成PNG或JPG文件,然后可以用BitmapFactory把文件中的数据读进来再生成Bitmap对象。
保存的代码大概类似于这样:
try {
FileOutputStream out = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
具体的可以去查Bitmap和BitmapFactory的帮助文档。
‘肆’ Android ,base64转bitmap
1 把图像文件读如byte数组中。
2 然后调用EncodeBase64函数,把Byte数组传入,函数返回Base64的字符串。
以上即可完成Base64转换。
反方向
1 然后调用DecodeBase64函数,把Byte64字符串传入,函数返回Byte数组。
2 把Bye数组内容写入文件,文件名为bitmap位图的bpm文件即可。
‘伍’ Android Bitmap 与 Drawable之间的区别和转换
Android bitmap和drawable的区别和转换如下:
1.bitmap 转换 drawable
java">Bitmapbitmap=newBitmap(...);Drawabledrawable=newBitmapDrawable(bitmap);
//Drawabledrawable=newFastBitmapDrawable(bitmap);
2.Drawable to Bitmap
BitmapDrawable, FastBitmapDrawable直接用getBitmap
b. 其他类型的Drawable用Canvas画到一个bitmap上
Canvascanvas=newCanvas(bitmap)
drawable.draw(canvas);
Drawabled=ImagesList.get(0);Bitmapbitmap=((BitmapDrawable)d).getBitmap();
区别如下:
1.Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565、RGB888。作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低。
2.Drawable - 作为Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF、PNG、JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变、图形等。
另外还有如下相类似的格式:
Canvas - 名为画布,可以看作是一种处理过程,使用各种方法来管理Bitmap、GL或者Path路径,同时它可以配合Matrix矩阵类给图像做旋转、缩放等操作,同时Canvas类还提供了裁剪、选取等操作。
Paint - 可以把它看做一个画图工具,比如画笔、画刷。管理了每个画图工具的字体、颜色、样式。
‘陆’ 如何把png文件转为可用于android.graphics.BitmapFactory.decod
把包在linux下解包。改了在换进去
‘柒’ 求教,如何在Android中将图片转化为矩阵
Android本身的android.graphics.Bitmap实现
一般在Android使用图片,都会用到这个类,这个类中有一个函数:
getPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height)
Returns in pixels[] a of the data in the bitmap.
功能和你要求的类似,只不过返回的是一位数组,转成二维数组应该不是问题吧。
使用是需注意,对于比较大的图片,你的操作很可能会比较占用内存,需要处理一下out of memory的exception。