二次采样及质量压缩

要记得添加SD卡的读取权限
参考地址:
 
 



package test.bawei.com.a0627;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import java.io.FileOutputStream;

import static android.graphics.BitmapFactory.decodeFile;

public class MainActivity extends AppCompatActivity {

    private ImageView image1,image2;
    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        image1 = (ImageView)findViewById(R.id.image1);
        image2 = (ImageView)findViewById(R.id.image2);

        getDate();
        getData2();

    }

    public void getDate() {

        String url = "/mnt/sdcard/a.jpg";

        bitmap= decodeFile(url);
        int width= bitmap.getWidth();
        int height= bitmap.getHeight();
        int count=bitmap.getRowBytes()*bitmap.getHeight();
        int count1= bitmap.getByteCount();

        try {
            FileOutputStream str = new FileOutputStream("/mnt/sdcard/aa_60.jpg");
            bitmap.compress(Bitmap.CompressFormat.JPEG,60,str);
            width= bitmap.getWidth();
            height= bitmap.getHeight();
            count=bitmap.getRowBytes()*bitmap.getHeight();
            count1= bitmap.getByteCount();

            bitmap= decodeFile("/mnt/sdcard/aa_60.jpg");
            image2.setImageBitmap(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void getData2() {
        BitmapFactory.Options options=new BitmapFactory.Options();

        options.inJustDecodeBounds=true;
        bitmap=  BitmapFactory.decodeFile("/mnt/sdcard/a.jpg",options);
        image1.setImageBitmap(bitmap);

        int h=  options.outHeight;
        int w=options.outWidth;

        int Size=1;

        while (h/Size>200||w/Size>300){
            Size*=2;

        }


        options.inJustDecodeBounds=false;

        options.inSampleSize=Size;

        options.inPreferredConfig=Bitmap.Config.ARGB_8888;
        bitmap= BitmapFactory.decodeFile("/mnt/sdcard/a.jpg",options);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值