如何从10MB图像COM preSS位从相机300KB beforw在android系统设置的ImageView [英] How to compress bitmap from 10mb image from camera to 300kb beforw setting to imageview in android

查看:119
本文介绍了如何从10MB图像COM preSS位从相机300KB beforw在android系统设置的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),bitmapOptions); 

终于有位来自摄像机的图像(我正在图像从高清晰度摄像机),这样大小可能是10MB。

finally bitmap has the image from camera (i am taking image from a high resolution camera ) so size may be 10mb.

我想设置ImageView的位图,如下

I am trying to set the bitmap in imageview as below

portfolioPicImgId.setImageBitmap(bitmap);


  • ,我想COM preSS影像分为300KB之前,我怎样才能实现
    这个!

  • 我见过的其他计算器的答案,但如何指定确切地
    300KB

  • 推荐答案

    你试过吗?

    //decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f){
        try {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);
    
            //The new size we want to scale to
            final int REQUIRED_SIZE=70;
    
            //Find the correct scale value. It should be the power of 2.
            int scale=1;
            while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
                scale*=2;
    
            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }
    

    这篇关于如何从10MB图像COM preSS位从相机300KB beforw在android系统设置的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆