android获取图片原始长宽

来源:互联网 发布:淘宝仓库发货流程 编辑:程序博客网 时间:2024/06/02 23:53
     在android里面要要取一张96 x 96像素每个像素点的RGB值然后处理,但是一开始直接读取
                Bitmap mBitmap =BitmapFactory.decodeResource(getResources(), R.drawable.jimg001);                int width=opts.outWidth;                int height=opts.outHeight;

     这样读出来的值却实变成了144 x 144像素,很无语,对android接触不多,查看各种资料才发现如果这样写android默认读取的不是原始大小

       BitmapFactory.Options opts = new BitmapFactory.Options();                opts.inJustDecodeBounds = true;                BitmapFactory.decodeResource(getResources(), R.drawable.jimg001, opts);                opts.inSampleSize = 1;                  opts.inJustDecodeBounds = false;                Bitmap mBitmap =BitmapFactory.decodeResource(getResources(), R.drawable.jimg001, opts);                int width=opts.outWidth;                int height=opts.outHeight;

      将BitmapFactory.Options.inSampleSize设为1才是原始大小,有时候根据需要因为图片太大而内存溢出时,需要适当选择inSampleSize的值,可以参考http://hi.baidu.com/liganggang/item/6ab1e480fa2da1e3e596e015