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 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;
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);
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)
// /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();
loadSdPic();
Bitmap bitmap=compassPic(picPath,320,320);
img2.setImageBitmap(bitmap);
}
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();
}
}
@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";
//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);
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");
}
//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 realHeight=options.outHeight;
int realWidth=options.outWidth;
Log.d("zzz","原始高度:"+realHeight+" 原始宽度:"+realWidth);
//二次:计算一个压缩比例///
int sampleSize=1;
if(realHeight>heightpx || realWidth>withpx){
if(realHeight>heightpx || realWidth>withpx){
int heighRate=realHeight/heightpx;
int widthRate=realWidth/withpx;
int widthRate=realWidth/withpx;
Log.d("zzz","heighRate:"+heighRate+" widthRate:"+widthRate);
//得到压缩比例
sampleSize=heighRate>widthRate?heighRate:widthRate;
sampleSize=heighRate>widthRate?heighRate:widthRate;
}
options.inJustDecodeBounds=false;
options.inSampleSize=sampleSize;//指定压缩比例 5
options.inPreferredConfig= Bitmap.Config.RGB_565;//1px 占2个字节
options.inSampleSize=sampleSize;//指定压缩比例 5
options.inPreferredConfig= Bitmap.Config.RGB_565;//1px 占2个字节
return BitmapFactory.decodeFile(picPath,options);
}
}