1package com.only.android.app; 2 3import java.io.File; 4 5import android.app.Activity; 6import android.app.AlertDialog; 7import android.content.DialogInterface; 8import android.content.Intent; 9import android.graphics.Bitmap; 10import android.graphics.BitmapFactory; 11import android.net.Uri; 12import android.os.Bundle; 13import android.os.SystemClock; 14import android.provider.MediaStore; 15import android.view.View; 16import android.widget.Button; 17import android.widget.ImageView; 18 19import com.only.android.R; 20 21public class CopyOfImageScaleActivity extends Activity implements View.OnClickListener { 22 /** Called when the activity is first created. */ 23 private Button selectImageBtn; 24 private ImageView imageView; 25 26 private File sdcardTempFile; 27 private AlertDialog dialog; 28 private int crop = 180; 29 30 @Override 31 public void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 setContentView(R.layout.imagescale); 34 35 selectImageBtn = (Button) findViewById(R.id.selectImageBtn); 36 imageView = (ImageView) findViewById(R.id.imageView); 37 38 selectImageBtn.setOnClickListener(this); 39 sdcardTempFile = new File("/mnt/sdcard/", "tmp_pic_" + SystemClock.currentThreadTimeMillis() + ".jpg"); 40 41 } 42 43 @Override 44 public void onClick(View v) { 45 if (v == selectImageBtn) { 46 if (dialog == null) { 47 dialog = new AlertDialog.Builder(this).setItems(new String[] { "相机", "相册" }, new DialogInterface.OnClickListener() { 48 @Override 49 public void onClick(DialogInterface dialog, int which) { 50 if (which == 0) { 51 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 52 intent.putExtra("output", Uri.fromFile(sdcardTempFile)); 53 intent.putExtra("crop", "true"); 54 intent.putExtra("aspectX", 1);// 裁剪框比例 55 intent.putExtra("aspectY", 1); 56 intent.putExtra("outputX", crop);// 输出图片大小 57 intent.putExtra("outputY", crop); 58 startActivityForResult(intent, 101); 59 } else { 60 Intent intent = new Intent("android.intent.action.PICK"); 61 intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*"); 62 intent.putExtra("output", Uri.fromFile(sdcardTempFile)); 63 intent.putExtra("crop", "true"); 64 intent.putExtra("aspectX", 1);// 裁剪框比例 65 intent.putExtra("aspectY", 1); 66 intent.putExtra("outputX", crop);// 输出图片大小 67 intent.putExtra("outputY", crop); 68 startActivityForResult(intent, 100); 69 } 70 } 71 }).create(); 72 } 73 if (!dialog.isShowing()) { 74 dialog.show(); 75 } 76 } 77 } 78 79 @Override 80 protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 81 if (resultCode == RESULT_OK) { 82 Bitmap bmp = BitmapFactory.decodeFile(sdcardTempFile.getAbsolutePath()); 83 imageView.setImageBitmap(bmp); 84 } 85 } 86}
Android从相机或相册获取图片裁剪
Stella981
2021-10-11
1181 0 0
点赞
收藏
评论区
加载中...