android获取图片尺寸的两种方法以及bitmap的缩放
16lz
2021-01-24
//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(安卓)View的小结
- weex android 选择文件 拍照 获得uri 并转为文件真实路径 转base
- chromium判断是否支持硬件加速的方法
- Android(安卓)学习笔记(二) 各种技巧小知识
- Android:自定义控件和使用方法
- Android中定义接口的方法
- BitMap、Drawable、inputStream及byte[] 互转
- AsyncTaskLoader使用方法
- Android中常用方法的归纳集合总结(二)