android系统裁剪图片并保存 <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
package com.airon.photo.show; import android.Manifest; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import com.airon.photo.R; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends Activity { public static final int REQUEST_CODE_CHOOSE_IMAGE = 1; public static final int REQUEST_CODE_CROP_IMAGE = 2; private Button picture; private Uri iconUri; private Uri cropImageUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ac_main); requestAllPower();//获取动态权限 picture = findViewById(R.id.picture); picture.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { click(); } }); } //按钮点击事件 public void click() { Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent, REQUEST_CODE_CHOOSE_IMAGE); } //请求应用所需所有权限 public void requestAllPower() { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1); } /** * 裁减图片操作 * * @param */ private void startCropImage(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 使图片处于可裁剪状态 intent.putExtra("crop", "true"); // 裁剪框的比例(根据需要显示的图片比例进行设置) if (Build.MANUFACTURER.contains("HUAWEI")) { //硬件厂商为华为的,默认是圆形裁剪框,这里让它无法成圆形 intent.putExtra("aspectX", 9999); intent.putExtra("aspectY", 9998); }else{ //其他手机一般默认为方形 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); } // 设置裁剪区域的形状,默认为矩形,也可设置为圆形,可能无效 intent.putExtra("circleCrop", true); // 让裁剪框支持缩放 intent.putExtra("scale", true); // 传递原图路径 File cropFile = new File(Environment.getExternalStorageDirectory() + "/crop_image.jpg"); try { if (cropFile.exists()) { cropFile.delete(); } cropFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } cropImageUri = Uri.fromFile(cropFile); intent.putExtra(MediaStore.EXTRA_OUTPUT, cropImageUri); // 设置图片的输出格式 intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); // return-data=true传递的为缩略图,小米手机默认传递大图,所以会导致onActivityResult调用失败 intent.putExtra("return-data", false); startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (intent != null) { switch (requestCode) { // 将选择的图片进行裁剪 case REQUEST_CODE_CHOOSE_IMAGE: if (intent.getData() != null) { iconUri = intent.getData(); startCropImage(iconUri); } break; case REQUEST_CODE_CROP_IMAGE: if (resultCode == RESULT_OK) { try { Bitmap bitmap = BitmapFactory.decodeStream (getContentResolver() .openInputStream(cropImageUri)); saveImageToGallery(bitmap,"jpg"); } catch (FileNotFoundException e) { e.printStackTrace(); } } break; default: break; } } } //保存文件到指定路径保存到相册 public String saveImageToGallery(Bitmap bitmap, String type) { // 首先保存图片 String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "dearxy"; File appDir = new File(storePath); if (!appDir.exists()) { appDir.mkdir(); } String fileName = ""; if ("jpg".equals(type)) { fileName = System.currentTimeMillis() + ".jpg"; } else { fileName = System.currentTimeMillis() + ".png"; } File file = new File(appDir, fileName); try { FileOutputStream fos = new FileOutputStream(file); //通过io流的方式来压缩保存图片 if ("jpg".equals(type)) { bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); } else { bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); } fos.flush(); fos.close(); //保存图片后发送广播通知更新数据库 Uri uri = Uri.fromFile(file); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)); } catch (IOException e) { e.printStackTrace(); } return file.getAbsolutePath(); } }