11package com .clock .study .activity ;
22
3+ import android .Manifest ;
4+ import android .content .DialogInterface ;
35import android .content .Intent ;
6+ import android .content .pm .PackageManager ;
7+ import android .net .Uri ;
8+ import android .os .Build ;
49import android .os .Bundle ;
10+ import android .provider .Settings ;
11+ import android .support .annotation .NonNull ;
12+ import android .support .v4 .app .ActivityCompat ;
13+ import android .support .v7 .app .AlertDialog ;
514import android .support .v7 .app .AppCompatActivity ;
615import android .util .Log ;
716import android .view .View ;
17+ import android .widget .Toast ;
818
919import com .clock .study .R ;
1020import com .clock .study .helper .CapturePhotoHelper ;
@@ -23,6 +33,11 @@ public class CapturePhotoActivity extends AppCompatActivity implements View.OnCl
2333 private final static String TAG = CapturePhotoActivity .class .getSimpleName ();
2434 private final static String EXTRA_RESTORE_PHOTO = "extra_restore_photo" ;
2535
36+ /**
37+ * 运行时权限申请码
38+ */
39+ private final static int RUNTIME_PERMISSION_REQUEST_CODE = 0x1 ;
40+
2641 private CapturePhotoHelper mCapturePhotoHelper ;
2742 private File mRestorePhotoFile ;
2843
@@ -32,8 +47,6 @@ protected void onCreate(Bundle savedInstanceState) {
3247 super .onCreate (savedInstanceState );
3348 setContentView (R .layout .activity_camera_take_photo );
3449
35- mCapturePhotoHelper = new CapturePhotoHelper (this , FolderManager .getPhotoFolder ());
36-
3750 findViewById (R .id .iv_take_photo ).setOnClickListener (this );
3851
3952 }
@@ -42,29 +55,62 @@ protected void onCreate(Bundle savedInstanceState) {
4255 protected void onSaveInstanceState (Bundle outState ) {
4356 Log .i (TAG , "onSaveInstanceState" );
4457 super .onSaveInstanceState (outState );
45- mRestorePhotoFile = mCapturePhotoHelper .getPhoto ();
46- Log .i (TAG , "onSaveInstanceState , mRestorePhotoFile: " + mRestorePhotoFile );
47- if (mRestorePhotoFile != null ) {
48- outState .putSerializable (EXTRA_RESTORE_PHOTO , mRestorePhotoFile );
58+ if (mCapturePhotoHelper != null ) {
59+ mRestorePhotoFile = mCapturePhotoHelper .getPhoto ();
60+ Log .i (TAG , "onSaveInstanceState , mRestorePhotoFile: " + mRestorePhotoFile );
61+ if (mRestorePhotoFile != null ) {
62+ outState .putSerializable (EXTRA_RESTORE_PHOTO , mRestorePhotoFile );
63+ }
4964 }
50-
5165 }
5266
5367 @ Override
5468 protected void onRestoreInstanceState (Bundle savedInstanceState ) {
5569 Log .i (TAG , "onRestoreInstanceState" );
5670 super .onRestoreInstanceState (savedInstanceState );
57- mRestorePhotoFile = (File ) savedInstanceState .getSerializable (EXTRA_RESTORE_PHOTO );
58- Log .i (TAG , "onRestoreInstanceState , mRestorePhotoFile: " + mRestorePhotoFile );
59- mCapturePhotoHelper .setPhoto (mRestorePhotoFile );
71+ if (mCapturePhotoHelper != null ) {
72+ mRestorePhotoFile = (File ) savedInstanceState .getSerializable (EXTRA_RESTORE_PHOTO );
73+ Log .i (TAG , "onRestoreInstanceState , mRestorePhotoFile: " + mRestorePhotoFile );
74+ mCapturePhotoHelper .setPhoto (mRestorePhotoFile );
75+ }
6076 }
6177
6278 @ Override
6379 public void onClick (View v ) {
6480 int viewId = v .getId ();
6581 if (viewId == R .id .iv_take_photo ) {
66- mCapturePhotoHelper .capture ();
82+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) { //Android M 处理Runtime Permission
83+ if (ActivityCompat .checkSelfPermission (this , Manifest .permission .WRITE_EXTERNAL_STORAGE ) == PackageManager .PERMISSION_GRANTED ) {//检查是否有写入SD卡的授权
84+ Log .i (TAG , "granted permission!" );
85+ turnOnCamera ();
86+ } else {
87+ Log .i (TAG , "denied permission!" );
88+ if (ActivityCompat .shouldShowRequestPermissionRationale (this , Manifest .permission .WRITE_EXTERNAL_STORAGE )) {
89+ Log .i (TAG , "should show request permission rationale!" );
90+ }
91+ requestPermission ();
92+ }
93+ } else {
94+ turnOnCamera ();
95+ }
96+ }
97+ }
98+
99+ /**
100+ * 开启相机
101+ */
102+ private void turnOnCamera () {
103+ if (mCapturePhotoHelper == null ) {
104+ mCapturePhotoHelper = new CapturePhotoHelper (this , FolderManager .getPhotoFolder ());
67105 }
106+ mCapturePhotoHelper .capture ();
107+ }
108+
109+ /**
110+ * 申请写入sd卡的权限
111+ */
112+ private void requestPermission () {
113+ ActivityCompat .requestPermissions (this , new String []{Manifest .permission .WRITE_EXTERNAL_STORAGE }, RUNTIME_PERMISSION_REQUEST_CODE );
68114 }
69115
70116 @ Override
@@ -88,4 +134,58 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
88134 }
89135 }
90136
137+ @ Override
138+ public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
139+ super .onRequestPermissionsResult (requestCode , permissions , grantResults );
140+ if (requestCode == RUNTIME_PERMISSION_REQUEST_CODE ) {
141+ for (int index = 0 ; index < permissions .length ; index ++) {
142+ String permission = permissions [index ];
143+ if (Manifest .permission .WRITE_EXTERNAL_STORAGE .equals (permission )) {
144+ if (grantResults [index ] == PackageManager .PERMISSION_GRANTED ) {
145+ Log .i (TAG , "onRequestPermissionsResult: permission is granted!" );
146+ turnOnCamera ();
147+
148+ } else {
149+ showMissingPermissionDialog ();
150+
151+ }
152+ }
153+ }
154+ }
155+ }
156+
157+ /**
158+ * 显示打开权限提示的对话框
159+ */
160+ private void showMissingPermissionDialog () {
161+ AlertDialog .Builder builder = new AlertDialog .Builder (this );
162+ builder .setTitle (R .string .help );
163+ builder .setMessage (R .string .help_content );
164+
165+ builder .setNegativeButton (R .string .cancel , new DialogInterface .OnClickListener () {
166+ @ Override
167+ public void onClick (DialogInterface dialog , int which ) {
168+ Toast .makeText (CapturePhotoActivity .this , R .string .camera_open_error , Toast .LENGTH_SHORT ).show ();
169+ finish ();
170+ }
171+ });
172+
173+ builder .setPositiveButton (R .string .settings , new DialogInterface .OnClickListener () {
174+ @ Override
175+ public void onClick (DialogInterface dialog , int which ) {
176+ turnOnSettings ();
177+ }
178+ });
179+
180+ builder .show ();
181+ }
182+
183+ /**
184+ * 启动系统权限设置界面
185+ */
186+ private void turnOnSettings () {
187+ Intent intent = new Intent (Settings .ACTION_APPLICATION_DETAILS_SETTINGS );
188+ intent .setData (Uri .parse ("package:" + getPackageName ()));
189+ startActivity (intent );
190+ }
91191}
0 commit comments