提取SD卡中的图片

时间:2021-07-29 10:54:01

读取SD卡的权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

系统跳转
startActivityForResult(new Intent(Intent.ACTION_PICK).setType("image/*"),PICK_CODE);

回来后取得图片
 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==PICK_CODE){
if(data!=null){
// 获取图片地址
//
Cursor cursor=getContentResolver().query(data.getData(), null, null, null, null);
cursor.moveToFirst();
currentPhotoStr=cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
cursor.close();
// 压缩图片
resizePhoto();
imageView.setImageBitmap(photoImage);
}
}
} 压缩图片
private void resizePhoto() {
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeFile(currentPhotoStr,options);
double ratio=Math.max(options.outWidth*1.0d/1024f,options.outHeight*1.0d/1024f);
options.inSampleSize=(int)Math.ceil(ratio);
options.inJustDecodeBounds=false;
photoImage= BitmapFactory.decodeFile(currentPhotoStr, options);
}