forked from D-clock/AndroidStudyCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCapturePhotoActivity.java
More file actions
191 lines (166 loc) · 6.65 KB
/
CapturePhotoActivity.java
File metadata and controls
191 lines (166 loc) · 6.65 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.clock.study.activity;
import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.clock.study.R;
import com.clock.study.helper.CapturePhotoHelper;
import com.clock.study.manager.FolderManager;
import java.io.File;
/**
* 调用系统相机进行拍照
*
* @author Clock
* @since 2016-05-13
*/
public class CapturePhotoActivity extends AppCompatActivity implements View.OnClickListener {
private final static String TAG = CapturePhotoActivity.class.getSimpleName();
private final static String EXTRA_RESTORE_PHOTO = "extra_restore_photo";
/**
* 运行时权限申请码
*/
private final static int RUNTIME_PERMISSION_REQUEST_CODE = 0x1;
private CapturePhotoHelper mCapturePhotoHelper;
private File mRestorePhotoFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_take_photo);
findViewById(R.id.iv_take_photo).setOnClickListener(this);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.i(TAG, "onSaveInstanceState");
super.onSaveInstanceState(outState);
if (mCapturePhotoHelper != null) {
mRestorePhotoFile = mCapturePhotoHelper.getPhoto();
Log.i(TAG, "onSaveInstanceState , mRestorePhotoFile: " + mRestorePhotoFile);
if (mRestorePhotoFile != null) {
outState.putSerializable(EXTRA_RESTORE_PHOTO, mRestorePhotoFile);
}
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.i(TAG, "onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState);
if (mCapturePhotoHelper != null) {
mRestorePhotoFile = (File) savedInstanceState.getSerializable(EXTRA_RESTORE_PHOTO);
Log.i(TAG, "onRestoreInstanceState , mRestorePhotoFile: " + mRestorePhotoFile);
mCapturePhotoHelper.setPhoto(mRestorePhotoFile);
}
}
@Override
public void onClick(View v) {
int viewId = v.getId();
if (viewId == R.id.iv_take_photo) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //Android M 处理Runtime Permission
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {//检查是否有写入SD卡的授权
Log.i(TAG, "granted permission!");
turnOnCamera();
} else {
Log.i(TAG, "denied permission!");
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Log.i(TAG, "should show request permission rationale!");
}
requestPermission();
}
} else {
turnOnCamera();
}
}
}
/**
* 开启相机
*/
private void turnOnCamera() {
if (mCapturePhotoHelper == null) {
mCapturePhotoHelper = new CapturePhotoHelper(this, FolderManager.getPhotoFolder());
}
mCapturePhotoHelper.capture();
}
/**
* 申请写入sd卡的权限
*/
private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, RUNTIME_PERMISSION_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "requestCode: " + requestCode + " resultCode: " + resultCode + " data: " + data);
if (requestCode == CapturePhotoHelper.CAPTURE_PHOTO_REQUEST_CODE) {
File photoFile = mCapturePhotoHelper.getPhoto();
if (photoFile != null) {
if (resultCode == RESULT_OK) {
PhotoPreviewActivity.preview(this, photoFile);
finish();
} else {
if (photoFile.exists()) {
photoFile.delete();
}
}
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == RUNTIME_PERMISSION_REQUEST_CODE) {
for (int index = 0; index < permissions.length; index++) {
String permission = permissions[index];
if (Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permission)) {
if (grantResults[index] == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "onRequestPermissionsResult: permission is granted!");
turnOnCamera();
} else {
showMissingPermissionDialog();
}
}
}
}
}
/**
* 显示打开权限提示的对话框
*/
private void showMissingPermissionDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.help);
builder.setMessage(R.string.help_content);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(CapturePhotoActivity.this, R.string.camera_open_error, Toast.LENGTH_SHORT).show();
finish();
}
});
builder.setPositiveButton(R.string.settings, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
turnOnSettings();
}
});
builder.show();
}
/**
* 启动系统权限设置界面
*/
private void turnOnSettings() {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
}