没有合适的资源?快使用搜索试试~ 我知道了~
android获取图片尺寸的两种方式及bitmap的缩放操作
1 下载量 188 浏览量
2021-01-03
10:30:29
上传
评论
收藏 57KB PDF 举报
温馨提示
试读
2页
我就废话不多说了,大家还是直接看代码吧~ //Uri.parse(file://+result.getImage().getCompressPath())) String path=uri.getPath(); Log.e(图片路径,path+); SpannableString spannableString=new SpannableString(path); //方法一:通过uri把图片转化为bitmap的方法 Bitmap bitmap= BitmapFactory.decodeFile(path); int height= bitmap.get
资源推荐
资源详情
资源评论
android获取图片尺寸的两种方式及获取图片尺寸的两种方式及bitmap的缩放操作的缩放操作
我就废话不多说了,大家还是直接看代码吧~
//Uri.parse("file://"+result.getImage().getCompressPath()))
String path=uri.getPath();
Log.e("图片路径",path+"");
SpannableString spannableString=new SpannableString(path);
//方法一:通过uri把图片转化为bitmap的方法
Bitmap bitmap= BitmapFactory.decodeFile(path);
int height= bitmap.getHeight();
int width= bitmap.getWidth();
Log.e("通过bitmap获取到的图片大小","width:"+width+"height"+height);
//方法二:使用Options类来获取
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;//这个参数设置为true才有效,
Bitmap bmp = BitmapFactory.decodeFile(path, options);//这里的bitmap是个空
if(bmp==null){
Log.e("通过options获取到的bitmap为空","===");
}
int outHeight=options.outHeight;
int outWidth= options.outWidth;
Log.e("通过Options获取到的图片大小","width:"+outWidth+"height"+outHeight);
关于两种方法:关于两种方法:
第一种:第一种: 直接把bitmap加载到内存中,通过对bitmap的测量,得出宽高,由于这个方法直接把图片引入内存,如果图片过
大,将会引发OOM;
第二种第二种:bitmap.options类为bitmap的裁剪类,通过他可以实现bitmap的裁剪;如果不设置裁剪后的宽高和裁剪比例,返回的
bitmap对象将为空,但是这个对象存储了原bitmap的宽高信息。
打log输出信息如下:
缩放:
Bitmap bitmap=null;
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=2;
options.inJustDecodeBounds = false;
if(path.equals("a1")){
bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.a1,options);
inSampleSize表示缩放比例
补充知识:补充知识:android获取获取Bitmap对象,获取图片宽高对象,获取图片宽高
android在不加载图片的前提下获得图片的宽高
public static int[] getImageWidthHeight(String path){
BitmapFactory.Options options = new BitmapFactory.Options();
/**
* 最关键在此,把options.inJustDecodeBounds = true;
* 这里再decodeFile(),返回的bitmap为空,但此时调用options.outHeight时,已经包含了图片的高了
*/
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回的bitmap为null
/**
*options.outHeight为原始图片的高
*/
return new int[]{options.outWidth,options.outHeight};
}
通过通过BitmapFactory从不同位置获取从不同位置获取Bitmap
资源评论
weixin_38683895
- 粉丝: 6
- 资源: 899
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功