Android开发之从相机或相册获取图片裁剪【转】

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

 

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!
上一篇:《树莓派Python编程入门与实战(第2版)》——3.7 创建Python脚本


下一篇:Java script中的函数使用方法