图片的二次采样

package animtest.com.example.e531.myapplication;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private ImageView img1,img2;
    private  String picPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img1= (ImageView) findViewById(R.id.img1);
        img2= (ImageView) findViewById(R.id.img2);

        //E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException:
        // /storage/emulated/0/aa.JPG: open failed: EACCES (Permission denied)


        //判断 读取 sd的权限有没有被授权
        int a= ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE);
        if(a!= PackageManager.PERMISSION_GRANTED){
            //进行请求
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE},100);

        }else{//如果已经被授权
            loadSdPic();

            Bitmap bitmap=compassPic(picPath,320,320);
            img2.setImageBitmap(bitmap);
        }


    }

    //得到请求的结果
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode==100){
            if(grantResults.length>0 ){
                if(grantResults[0]==PackageManager.PERMISSION_GRANTED && grantResults[1]==PackageManager.PERMISSION_GRANTED){
                    loadSdPic();
                    Bitmap bitmap=compassPic(picPath,320,320);
                    img2.setImageBitmap(bitmap);
                }else{
                    Toast.makeText(MainActivity.this,"权限被拒绝!", Toast.LENGTH_LONG).show();
                }
            }

        }
    }

    public void loadSdPic(){
        //img1加载sd上的图片
        //1.判断sd卡是否可用
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            //2.得到图片的路径
            picPath=Environment.getExternalStorageDirectory().getPath()+ File.separator+"aa.JPG";

            //3.显示
            Bitmap bitmap=BitmapFactory.decodeFile(picPath);
            img1.setImageBitmap(bitmap);

        }

    }

    public  Bitmap compassPic(String picPath,int withpx,int heightpx){
        //1.创建options对旬
        BitmapFactory.Options options=new BitmapFactory.Options();
        //2.
        options.inJustDecodeBounds=true;
        Bitmap b=BitmapFactory.decodeFile(picPath,options);
        if(b==null){
            Log.d("zzz"," is null");
        }

        //得到原始图片的宽度与高度
        int realHeight=options.outHeight;
        int realWidth=options.outWidth;
        Log.d("zzz","原始高度:"+realHeight+" 原始宽度:"+realWidth);
        //二次:计算一个压缩比例///

        int sampleSize=1;
        if(realHeight>heightpx || realWidth>withpx){

            int heighRate=realHeight/heightpx;
            int widthRate=realWidth/withpx;

            Log.d("zzz","heighRate:"+heighRate+" widthRate:"+widthRate);

            //得到压缩比例
            sampleSize=heighRate>widthRate?heighRate:widthRate;

        }

        options.inJustDecodeBounds=false;
        options.inSampleSize=sampleSize;//指定压缩比例 5
        options.inPreferredConfig= Bitmap.Config.RGB_565;//1px 占2个字节


        return BitmapFactory.decodeFile(picPath,options);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值