在实际应用中,经常需要对图片进行处理,包括压缩、截图等等,其实android系统提供了一个可以截图的activity,我们只需调用它就行了,下面示例完成一个通过相机照相或相册选取图片后对其截图的功能,其实知道图片的路劲都可以调用截图activity,代码如下:
测试activity:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import
java.io.File;
import
android.app.Activity;
import
android.content.Intent;
import
android.graphics.Bitmap;
import
android.graphics.drawable.BitmapDrawable;
import
android.net.Uri;
import
android.os.Bundle;
import
android.os.Environment;
import
android.provider.MediaStore;
import
android.provider.MediaStore.Images.Media;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.ImageView;
import
android.widget.Toast;
public
class
TestClipImageActivity extends
Activity implements
OnClickListener {
privateButton gallaryBtn;
privateButton cameraBtn;
privateImageView showImage;
privateFile file;
@Override 
protectedvoid
onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    gallaryBtn= (Button) findViewById(R.id.main_btn_gallery);
    cameraBtn= (Button) findViewById(R.id.main_btn_camera);
    gallaryBtn.setOnClickListener(this); 
    cameraBtn.setOnClickListener(this); 
    showImage= (ImageView) findViewById(R.id.main_image);
} 
@Override 
publicvoid
onClick(View v) { 
    if(v == gallaryBtn) {
        //跳转至相册界面
        Intentintent = new
Intent(Intent.ACTION_PICK,
                Media.EXTERNAL_CONTENT_URI); 
        startActivityForResult(intent, 0); 
    } 
    if(v == cameraBtn) {
        if(!Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())){
            Toast.makeText(this, "请插入SD卡",Toast.LENGTH_SHORT).show();
            return; 
        } 
        //跳转至相机界面
        Intentintent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        file= new
File(Environment.getExternalStorageDirectory(),
                getPhotoName()); 
        //指定相机拍照后相片的存储位置
        intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(file));
        startActivityForResult(intent, 1); 
    } 
} 
@Override 
protectedvoid
onActivityResult(int
requestCode, int
resultCode, Intent data) {
    super.onActivityResult(requestCode,resultCode, data);
    //系统相册返回
    if(requestCode == 0
&& resultCode == Activity.RESULT_OK
            &&data != null)
{ 
        startPhotoZoom(data.getData(), 150);//截图
    } 
    //系统相机返回
    if(requestCode == 1
&& resultCode == Activity.RESULT_OK) {
        startPhotoZoom(Uri.fromFile(file), 150);//截图
    } 
    //截图后返回
    if(requestCode == 2
&& data != null)
{ 
        Bundlebundle = data.getExtras();
        if(bundle != null)
{ 
            Bitmapbitmap = bundle.getParcelable("data");
            showImage.setBackgroundDrawable(newBitmapDrawable(bitmap));
        } 
    } 
} 
/**
 *跳转至系统截图界面进行截图
 * 
 *@param data
 *@param size
 */ 
privatevoid
startPhotoZoom(Uri data, int
size) {
    Intentintent = new
Intent("com.android.camera.action.CROP");
    intent.setDataAndType(data, "image/*"); 
    //crop为true时表示显示的view可以剪裁
    intent.putExtra("crop", "true"); 
    //aspectX aspectY 是宽高的比例
    intent.putExtra("aspectX", 1); 
    intent.putExtra("aspectY", 1); 
    //outputX,outputY 是剪裁图片的宽高
    intent.putExtra("outputX",size);
    intent.putExtra("outputY",size);
    intent.putExtra("return-data", true); 
    startActivityForResult(intent, 2); 
} 
/** 
 *产生照片名称
 *  
 *@return
 */ 
privateString getPhotoName() {
    Stringname = System.currentTimeMillis() + ".jpg";
    returnname;
} }
布局文件:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical">
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:orientation="horizontal">
    <Button 
        android:id="@+id/main_btn_gallery" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="相册"/>
    <Button 
        android:id="@+id/main_btn_camera" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="相机"/>
</LinearLayout> 
<ImageView 
    android:id="@+id/main_image" 
    android:layout_width="150dp" 
    android:layout_height="150dp" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="30dp"/>
</LinearLayout>
加上权限:
?
1
2
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>