Android获取bitmap的常用方法(R文件、uri、String类型路径及Glide)

1.以R文件的方式获取,即从…/main/res/drawable/a.jpg中获取图片对应的bitmap

第一种:
Bitmap bp = BitmapFactory.decodeResource(this.getBaseContext().getResources(), R.drawable.a);
第二种:
BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.a);
Bitmap bitmap = bitmapDrawable.getBitmap();
第三种:
Bitmap bp=BitmapFactory.decodeStream(getClass().getResourceAsStream("/res/drawable/a.jpg"));

2.通过uri(网页或本地地址)获取bitmap,

第一种:
Uri mImageCaptureUri = data.getData();
Bitmap photoBmp = null;
if (mImageCaptureUri != null) {
    photoBmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mImageCaptureUri);
}
第二种:
bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri));
第三种(获取过程中以2为除数对宽高进行缩放):
    private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException
    {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);
        // The new size we want to scale to
        final int REQUIRED_SIZE =500;//700
        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
               || height_tmp / 2 < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }
        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
    }

3.通过String类型的photopath获取bitmap,

    private Bitmap getDiskBitmap(String pathString){
        Bitmap bitmap = null;
        try{
            File file=new File(pathString);
            if (file.exists()){
                bitmap= BitmapFactory.decodeFile(pathString);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return bitmap;
}

4.通过框架Glide获取并显示

            ImageView iv_photo;
            String photoPath;
            private File cameraSavePath;
            cameraSavePath = new File(Environment.getExternalStorageDirectory().getPath() + "/" + System.currentTimeMillis() +".jpg");
            photoPath = String.valueOf(cameraSavePath);
            Glide.with(this).load(photoPath).apply(RequestOptions.noTransformation()
            .override(iv_photo.getWidth(),iv_photo.getHeight())
            .error(R.drawable.default_person_icon))
            .into(iv_photo);
第二种:
https://blog.csdn.net/wl724120268/article/details/79552196 
第三种:
http://www.cppcns.com/ruanjian/android/213221.html           

参考:
获取Bitmap的几种方式总结
通过路径获取本地图片bitmap

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值