Skip to content

Commit d6697a5

Browse files
committed
修复bug,处理兼容问题,详见README.md
1 parent dabf645 commit d6697a5

5 files changed

Lines changed: 137 additions & 14 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
- 本库libs下有个**AndroidUtils.aar**的依赖包,是由[AndroidUtils](https://github.com/D-clock/AndroidUtils)代码编译生成;
99
- 想要查看**AndroidUtils.aar**中的源代码,可以参考[这里](https://github.com/D-clock/Doc/blob/master/Android/%E4%B8%AA%E4%BA%BA%E6%94%B6%E8%97%8F/%E5%A6%82%E4%BD%95%E6%9F%A5%E7%9C%8Baar%E7%9A%84%E6%BA%90%E4%BB%A3%E7%A0%81.md)
1010

11-
## 最新更新(编辑于2016-05-27)
11+
## 最新更新(编辑于2016-06-08)
1212

13-
- 优化拍照实现;
14-
- 优化项目导入体验;
13+
- 兼容处理 Android M 版本的 Runtime Permission ;
14+
- 优化 build.gradle ,提高导入 moudle 体验;
1515

1616
## 归档文章
1717

‎build.gradle‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,15 @@ dependencies {
3232
//compile project(":AndroidUtils")
3333
compile 'com.android.support:appcompat-v7:23.2.1'
3434
}
35+
36+
37+
buildscript {
38+
repositories {
39+
jcenter()
40+
}
41+
dependencies {
42+
classpath 'com.android.tools.build:gradle:2.1.0'
43+
// NOTE: Do not place your application dependencies here; they belong
44+
// in the individual module build.gradle files
45+
}
46+
}

‎src/main/java/com/clock/study/activity/CapturePhotoActivity.java‎

Lines changed: 111 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
package com.clock.study.activity;
22

3+
import android.Manifest;
4+
import android.content.DialogInterface;
35
import android.content.Intent;
6+
import android.content.pm.PackageManager;
7+
import android.net.Uri;
8+
import android.os.Build;
49
import 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;
514
import android.support.v7.app.AppCompatActivity;
615
import android.util.Log;
716
import android.view.View;
17+
import android.widget.Toast;
818

919
import com.clock.study.R;
1020
import 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
}

‎src/main/java/com/clock/study/manager/FolderManager.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package com.clock.study.manager;
22

3+
import android.Manifest;
4+
import android.app.AlertDialog;
5+
import android.content.Context;
6+
import android.content.pm.PackageManager;
37
import android.os.Environment;
8+
import android.support.v4.app.ActivityCompat;
9+
10+
import com.clock.study.R;
411

512
import java.io.File;
613

‎src/main/res/values/strings.xml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@
55
<string name="take_photo">TakePhoto</string>
66
<string name="display_to_gallery">显示到相册</string>
77
<string name="not_specify_a_directory">未指定存储目录</string>
8+
<string name="help">帮助</string>
9+
<string name="help_content">当前权限被禁用,建议到设置界面开启权限!</string>
10+
<string name="cancel">取消</string>
11+
<string name="settings">设置</string>
812
</resources>

0 commit comments

Comments
 (0)