Android6.0 动静权限适配
Android6.0 开始,权限分为一般权限和危险权限,危险权限:会授予利用拜访用户的秘密数据的权限。如果你的利用在其清单文件中列出了危险权限,用户批准你的利用试验这些权限。
通过 adb 命令
F:\sodaoProject\automate>adb shell
cepheus:/ $ pm list permissions -g -d
Dangerous Permissions:
// 波及读写联系人,拜访账户
group:android.permission-group.CONTACTS
permission:android.permission.WRITE_CONTACTS
permission:android.permission.GET_ACCOUNTS
permission:android.permission.READ_CONTACTS
// 波及电话操作
group:android.permission-group.PHONE
permission:android.permission.READ_CALL_LOG
permission:android.permission.READ_PHONE_STATE
permission:android.permission.CALL_PHONE
permission:android.permission.WRITE_CALL_LOG
permission:android.permission.USE_SIP
permission:android.permission.PROCESS_OUTGOING_CALLS
permission:com.android.voicemail.permission.ADD_VOICEMAIL
// 波及日历信息的操作(用户日程安排)group:android.permission-group.CALENDAR
permission:android.permission.READ_CALENDAR
permission:android.permission.WRITE_CALENDAR
// 波及相机操作
group:android.permission-group.CAMERA
permission:android.permission.CAMERA
// 波及应用手机传感器操作
group:android.permission-group.SENSORS
permission:android.permission.BODY_SENSORS
// 波及用户地理位置信息的操作
group:android.permission-group.LOCATION
permission:android.permission.ACCESS_FINE_LOCATION
permission:android.permission.ACCESS_COARSE_LOCATION
// 波及存储卡的读写操作
group:android.permission-group.STORAGE
permission:android.permission.READ_EXTERNAL_STORAGE
permission:android.permission.WRITE_EXTERNAL_STORAGE
// 波及多媒体信息的操作
group:android.permission-group.MICROPHONE
permission:android.permission.RECORD_AUDIO
// 波及 SMS 卡的操作
group:android.permission-group.SMS
permission:android.permission.READ_SMS
permission:android.permission.RECEIVE_WAP_PUSH
permission:android.permission.RECEIVE_MMS
permission:android.permission.RECEIVE_SMS
permission:android.permission.SEND_SMS
permission:android.permission.READ_CELL_BROADCASTS
ungrouped:
permission:com.xiaomi.xmsf.permission.PAYMENT // 小米 下载而不显示告诉
permission:miui.permission.ACCESS_BLE_SETTINGS // 小米 波及到用户设置的操作
一:权限申请波及到几个办法
1. 查看权限
ContextCompat.checkSelfPermission(Context context, String permission)
例子:检测联系人权限
(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
返回值(package android.content.pm 的 PackageManager 中的常量)
1. 有权限: PackageManager.PERMISSION_GRANTED 值:0 已受权
2. 无权限: PackageManager.PERMISSION_DENIED 值:- 1 未受权
2. 解释权限
ActivityCompat.shouldShowRequestPermissionRationale(Activity activity, String permission)
例子:ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_CONTACTS)~~~~
(1)判断是否有必要向用户解释为什么要这项权限,如果第一次申请此权限,但用户回绝了,则之后调用该办法将返回 true, 此时有必要向用户具体阐明须要此权限的起因
(2) 如果利用第一次回绝了申请此权限、第二次再申请此权限时,用户勾选了权限申请对话框“不在询问”,则此办法返回 false. 如果设施标准禁止利用领有该权限,此办法返回也是 false
3. 申请权限
ActivityCompat.requestPermissions(Activity activity, String[] permissions, int requestCode)
例子:ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},MY_PERMISSIONS_REQUEST_READ_CONTACTS);
(1): 权限参数传入的是数组,能够调用该办法一次申请多个权限;
(2): 传入的权限数组参数以单个具体权限为单位,但弹框询问用户受权时,属于同一权限组的权限将主动合并询问受权一次;
(3): 申请的权限必须当时在 AndroidManifest.xml 中有申明,否则调用此办法申请时,将不弹框,而是间接返回“回绝”的后果;
(4): 第一次申请权限时,用户点击了“回绝”,第二次再申请该权限时,对话框将呈现“不再询问”复选框,如果用户勾选了“不再询问”并点击了“回绝”,则之后再申请此权限组时将不弹框,而是间接返回“回绝”的后果。
4. 解决申请后果
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){// 重写申请后果}
例子:@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// 重写 onRequestPermissionsResult 办法依据用户的不同抉择做出响应
switch (requestCode){
case MY_PERMISSIONS_REQUEST_READ_CONTACTS:
if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){Log.d(TAG,"onRequestPermissionsResult granted");
}else {Log.d(TAG,"onRequestPermissionsResult denied");
showWaringDialog();// 提醒设置开启权限弹窗}
break;
}
实现代码:
private void requestPermission() {Log.d(TAG,"requestPermission");
// 读取联系人的权限 <uses-permission android:name="android.permission.READ_CONTACTS" />
// 判断权限是否受权
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){Log.d(TAG,"未受权");
// 解释为什么须要受权该权限
if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)){Log.d(TAG,"解释");
// 申请受权此权限
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}else {Log.d(TAG,"没有解释");
// 申请受权此权限
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}
}else {Log.d(TAG,"曾经受权");
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// 重写 onRequestPermissionsResult 办法依据用户的不同抉择做出响应
switch (requestCode){
case MY_PERMISSIONS_REQUEST_READ_CONTACTS:
if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){Log.d(TAG,"onRequestPermissionsResult granted");
}else {Log.d(TAG,"onRequestPermissionsResult denied");
showWaringDialog();}
break;
}
}
private void showWaringDialog() {AlertDialog dialog=new AlertDialog.Builder(this)
.setTitle("提醒!")
.setMessage("请返回设置 -> 利用 ->TestW-> 权限中关上相干权限,否则性能无奈失常运行!")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 个别状况下,如果用户不受权的话,性能是无奈运行的,做出退出解决
finish();}
})
.show();}
编写一个权限申请工具类
public class PermissionUtils {private static final String TAG=PermissionUtils.class.getSimpleName();
// 九大危险权限 dangerous
// 照相机
public static final String PERMISSION_CAMERA= Manifest.permission.CAMERA;
// 读写文件
public static final String PERMISSION_READ_EXTERNAL_STORAGE=Manifest.permission.READ_EXTERNAL_STORAGE;
public static final String PERMISSION_WRITE_EXTERNAL_STORAGE=Manifest.permission.WRITE_EXTERNAL_STORAGE;
// 地理位置
public static final String PERMISSION_ACCESS_FINE_LOCATION=Manifest.permission.ACCESS_FINE_LOCATION;
public static final String PERMISSION_ACCESS_COARSE_LOCATION=Manifest.permission.ACCESS_COARSE_LOCATION;
// 电话
public static final String PERMISSION_CALL_PHONE=Manifest.permission.CALL_PHONE;
public static final String PERMISSION_READ_PHONE_STATE=Manifest.permission.READ_PHONE_STATE;
// 拜访账户,读写联系人
public static final String PERMISSION_GET_ACCOUNTS=Manifest.permission.GET_ACCOUNTS;
// 多媒体
public static final String PERMISSION_RECORD_AUDIO=Manifest.permission.RECORD_AUDIO;
// 权限汇合
private static final String[] requestPermissions={
PERMISSION_RECORD_AUDIO,
PERMISSION_GET_ACCOUNTS,
PERMISSION_READ_PHONE_STATE,
PERMISSION_CALL_PHONE,
PERMISSION_CAMERA,
PERMISSION_ACCESS_FINE_LOCATION,
PERMISSION_ACCESS_COARSE_LOCATION,
PERMISSION_READ_EXTERNAL_STORAGE,
PERMISSION_WRITE_EXTERNAL_STORAGE
};
public static final int CODE_RECORD_AUDIO = 0;
public static final int CODE_GET_ACCOUNTS = 1;
public static final int CODE_READ_PHONE_STATE = 2;
public static final int CODE_CALL_PHONE = 3;
public static final int CODE_CAMERA = 4;
public static final int CODE_ACCESS_FINE_LOCATION = 5;
public static final int CODE_ACCESS_COARSE_LOCATION = 6;
public static final int CODE_READ_EXTERNAL_STORAGE = 7;
public static final int CODE_WRITE_EXTERNAL_STORAGE = 8;
public static final int CODE_MULTI_PERMISSION = 100;
interface PermissionGrant {void onPermissionGranted(int requestCode);
}
// 申请权限
public static void requestPermission(final Activity activity,final int requestCode,PermissionGrant permissionGrant){if (activity==null){return;}
Log.d(TAG,"requestPermission requestCode:" + requestCode);
if (requestCode<0||requestCode>=requestPermissions.length){Log.w(TAG, "requestPermission illegal requestCode:" + requestCode);
return; }
final String requestPermission=requestPermissions[requestCode];
int checkSelfPermission;
try {checkSelfPermission= ActivityCompat.checkSelfPermission(activity,requestPermission);
}catch (RuntimeException e){Toast.makeText(activity, "please open this permission", Toast.LENGTH_SHORT)
.show();
Log.e(TAG, "RuntimeException:" + e.getMessage());
return; }
if (checkSelfPermission!= PackageManager.PERMISSION_GRANTED){Log.i(TAG, "ActivityCompat.checkSelfPermission != PackageManager.PERMISSION_GRANTED");
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,requestPermission)){Log.i(TAG, "requestPermission shouldShowRequestPermissionRationale");
shouldShowRationale(activity, requestCode, requestPermission);
}else {Log.d(TAG, "requestCameraPermission else");
ActivityCompat.requestPermissions(activity,new String[]{requestPermission},requestCode);
}
}else {Log.d(TAG, "ActivityCompat.checkSelfPermission ==== PackageManager.PERMISSION_GRANTED");
Toast.makeText(activity, "opened:" + requestPermissions[requestCode], Toast.LENGTH_SHORT).show();
permissionGrant.onPermissionGranted(requestCode);
}
}
private static void shouldShowRationale(Activity activity, int requestCode, String requestPermission) {String[] permissionsHint=activity.getResources().getStringArray(R.array.permissions);
showMessageOkCancel(activity, "Rationale:" + permissionsHint[requestCode], new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(activity,
new String[]{requestPermission},
requestCode);
Log.d(TAG, "showMessageOKCancel requestPermissions:" + requestPermission);
}
});
}
private static void showMessageOkCancel(final Activity context, String message, DialogInterface.OnClickListener okListener) {new AlertDialog.Builder(context)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();}
public static void requestPermissionsResult(final Activity activity, final int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults, PermissionGrant permissionGrant){if (activity==null){return;}
Log.d(TAG, "requestPermissionsResult requestCode:" + requestCode);
if (requestCode<0||requestCode>=requestPermissions.length){Log.w(TAG, "requestPermissionsResult illegal requestCode:" + requestCode);
Toast.makeText(activity, "illegal requestCode:" + requestCode, Toast.LENGTH_SHORT).show();
return; }
Log.i(TAG, "onRequestPermissionsResult requestCode:" + requestCode + ",permissions:" + permissions.toString()
+ ",grantResults:" + grantResults.toString() + ",length:" + grantResults.length);
if (grantResults.length==1&&grantResults[0]==PackageManager.PERMISSION_GRANTED){permissionGrant.onPermissionGranted(requestCode);
}else {String[] permissionsHint =activity.getResources().getStringArray(R.array.permissions);
openSettingActivity(activity, "Result" + permissionsHint[requestCode]);
}
}
// 开启设置界面
private static void openSettingActivity(Activity activity, String message) {showMessageOkCancel(activity, message, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Log.d(TAG, "getPackageName():" + activity.getPackageName());
Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
intent.setData(uri);
activity.startActivity(intent);
}
});
}
}
在 ThirdActivity 的 Activity 中
public class ThirdActivity extends AppCompatActivity{private static final String TAG = ThirdActivity.class.getSimpleName();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
}
public void showCamera(View view) {Log.i(TAG, "Show camera button pressed. Checking permission.");
PermissionUtils.requestPermission(this, PermissionUtils.CODE_CAMERA, mPermissionGrant);
}
public void getAccounts(View view) {PermissionUtils.requestPermission(this, PermissionUtils.CODE_GET_ACCOUNTS, mPermissionGrant);
}
public void callPhone(View view) {PermissionUtils.requestPermission(this, PermissionUtils.CODE_CALL_PHONE, mPermissionGrant);
}
public void readPhoneState(View view) {PermissionUtils.requestPermission(this, PermissionUtils.CODE_READ_PHONE_STATE, mPermissionGrant);
}
public void accessFineLocation(View view) {PermissionUtils.requestPermission(this, PermissionUtils.CODE_ACCESS_FINE_LOCATION, mPermissionGrant);
}
public void accessCoarseLocation(View view) {PermissionUtils.requestPermission(this, PermissionUtils.CODE_ACCESS_COARSE_LOCATION, mPermissionGrant);
}
public void readExternalStorage(View view) {PermissionUtils.requestPermission(this, PermissionUtils.CODE_READ_EXTERNAL_STORAGE, mPermissionGrant);
}
public void writeExternalStorage(View view) {PermissionUtils.requestPermission(this, PermissionUtils.CODE_WRITE_EXTERNAL_STORAGE, mPermissionGrant);
}
public void recordAudio(View view) {PermissionUtils.requestPermission(this, PermissionUtils.CODE_RECORD_AUDIO, mPermissionGrant);
}
private PermissionUtils.PermissionGrant mPermissionGrant=new PermissionUtils.PermissionGrant() {
@Override
public void onPermissionGranted(int requestCode) {switch (requestCode) {
case PermissionUtils.CODE_RECORD_AUDIO:
Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_RECORD_AUDIO", Toast.LENGTH_SHORT).show();
break; case PermissionUtils.CODE_GET_ACCOUNTS:
Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_GET_ACCOUNTS", Toast.LENGTH_SHORT).show();
break; case PermissionUtils.CODE_READ_PHONE_STATE:
Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_READ_PHONE_STATE", Toast.LENGTH_SHORT).show();
break; case PermissionUtils.CODE_CALL_PHONE:
Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_CALL_PHONE", Toast.LENGTH_SHORT).show();
break; case PermissionUtils.CODE_CAMERA:
Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_CAMERA", Toast.LENGTH_SHORT).show();
break; case PermissionUtils.CODE_ACCESS_FINE_LOCATION:
Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_ACCESS_FINE_LOCATION", Toast.LENGTH_SHORT).show();
break; case PermissionUtils.CODE_ACCESS_COARSE_LOCATION:
Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_ACCESS_COARSE_LOCATION", Toast.LENGTH_SHORT).show();
break; case PermissionUtils.CODE_READ_EXTERNAL_STORAGE:
Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_READ_EXTERNAL_STORAGE", Toast.LENGTH_SHORT).show();
break; case PermissionUtils.CODE_WRITE_EXTERNAL_STORAGE:
Toast.makeText(ThirdActivity.this, "Result Permission Grant CODE_WRITE_EXTERNAL_STORAGE", Toast.LENGTH_SHORT).show();
break; default:
break;
}
}
};
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, mPermissionGrant);
}
}
布局文件 activity_third 中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout android:id="@+id/content_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<ScrollView android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout 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:orientation="horizontal">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Camera"
android:id="@+id/button_camera"
android:onClick="showCamera"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RECORD_AUDIO"
android:onClick="recordAudio"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GET_ACCOUNTS"
android:onClick="getAccounts"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALL_PHONE"
android:onClick="callPhone"/>
</LinearLayout>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PERMISSION_READ_PHONE_STATE"
android:onClick="readPhoneState"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ACCESS_FINE_LOCATION"
android:onClick="accessFineLocation"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ACCESS_COARSE_LOCATION"
android:onClick="accessCoarseLocation"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="READ_EXTERNAL_STORAGE"
android:onClick="readExternalStorage"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WRITE_EXTERNAL_STORAGE"
android:onClick="writeExternalStorage"/>
</LinearLayout> </ScrollView>
</LinearLayout>
清单文件的申请权限
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
资源文件在 res 下 values 下创立一个 array.xml 文件
<resources>
<string-array name="permissions">
<item>@string/permission_recode_audio_hint</item>
<item>@string/permission_get_accounts_hint</item>
<item>@string/permission_read_phone_hint</item>
<item>@string/permission_call_phone_hint</item>
<item>@string/permission_camera_hint</item>
<item>@string/permission_access_fine_location_hint</item>
<item>@string/permission_access_coarse_location_hint</item>
<item>@string/permission_read_external_hint</item>
<item>@string/permission_white_external_hint</item>
</string-array>
</resources>
在 res 下 values 下的 strings.xml 文件创建
<resources>
<string name="my_accessibility_description"> 十分建议您开启无障碍服务,开启后能够帮助您实现一些简略的操作,晋升您的操作体验。</string>
<string name="permission_get_accounts_hint"> 没有此权限,无奈开启这个性能,请开启权限。PERMISSION_GET_ACCOUNTS</string>
<string name="permission_read_phone_hint"> 没有此权限,无奈开启这个性能,请开启权限。PERMISSION_READ_PHONE_STATE</string>
<string name="permission_call_phone_hint"> 没有此权限,无奈开启这个性能,请开启权限。PERMISSION_CALL_PHONE</string>
<string name="permission_camera_hint"> 没有此权限,无奈开启这个性能,请开启权限。PERMISSION_CAMERA</string>
<string name="permission_access_fine_location_hint"> 没有此权限,无奈开启这个性能,请开启权限。PERMISSION_ACCESS_FINE_LOCATION</string>
<string name="permission_access_coarse_location_hint"> 没有此权限,无奈开启这个性能,请开启权限。PERMISSION_ACCESS_COARSE_LOCATION</string>
<string name="permission_read_external_hint"> 没有此权限,无奈开启这个性能,请开启权限。PERMISSION_READ_EXTERNAL_STORAGE</string>
<string name="permission_white_external_hint"> 没有此权限,无奈开启这个性能,请开启权限。PERMISSION_WRITE_EXTERNAL_STORAGE</string>
<string name="permission_recode_audio_hint"> 没有此权限,无奈开启这个性能,请开启权限。PERMISSION_RECORD_AUDIO</string>
</resources>
小米零碎在 Android 原生的动静权限申请的根底上,还有本人的用户受权模块
能够看到小米的受权模块中,对权限操作分为容许,询问,回绝。
1. 当咱们第一次关上利用的时候,默认是询问状态,在该状态下,咱们调用 requestPermission()办法会弹出零碎询问框.
2. 在弹出零碎受权框后,只有你操作了(回绝或者容许), 你永远也不要想着在当前能看到受权框了,除非你过去设置这边更改为“询问”模式。
3. 不然无论你再调用几次 requestPermissions(),都是间接走回调 OnRequestPermissionResult。
Android7.0 调用相机拍照保留照片,就是对手机存储中公有文件门路的爱护
1. 在 Android7.0 之前中调用相机实现拍照保留照片
/**
* 调用零碎相机实现拍照 */
public void request(View view){if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){Toast.makeText(ThirdActivity.this,"SD 卡异样",Toast.LENGTH_LONG).show();
return; }
long dateTaken=System.currentTimeMillis();
// 图像名称
CharSequence fileName= DateFormat.format("yyyy-MM-dd kk.mm.ss",dateTaken);
// 图像门路
String path=Environment.getExternalStorageDirectory().toString()+ File.separator+"forpermission"+File.separator+fileName+".jpg";
File imageFile=new File(path);
if (!imageFile.getParentFile().exists()){imageFile.getParentFile().mkdirs();}
if (!imageFile.exists()){
try {imageFile.createNewFile();
} catch (IOException e) {e.printStackTrace();
}
}
// 依据文件解析出文件对应的 Uri
Uri uri=Uri.fromFile(imageFile); Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT,uri); // 依据文件解析出文件对应的 uri
// 判断是否有 Activity 能解决 intent
if (intent.resolveActivity(getPackageManager())!=null){startActivityForResult(intent,REQUEST_TAKE_PHOTO);
}
}
2. 返回后果
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK){if (requestCode==REQUEST_TAKE_PHOTO){Toast.makeText(this, "拍照胜利", Toast.LENGTH_SHORT).show();
Log.d(TAG,"拍照胜利了呀");
}
}
}
Android7.0 之后如果调用零碎相机拍照会报这个问题
Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/forpermission/2021-03-15%2014.30.48.jpg exposed beyond app through ClipData.Item.getUri()
须要提供 FileProvider
/**
* 调用零碎相机实现拍照 */
public void request(View view){if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){Toast.makeText(ThirdActivity.this,"SD 卡异样",Toast.LENGTH_LONG).show();
return; }
long dateTaken=System.currentTimeMillis();
// 图像名称
CharSequence fileName= DateFormat.format("yyyy-MM-dd kk.mm.ss",dateTaken);
// 图像门路
String path=Environment.getExternalStorageDirectory().toString()+ File.separator+"forpermission"+File.separator+fileName+".jpg";
File imageFile=new File(path);
if (!imageFile.getParentFile().exists()){imageFile.getParentFile().mkdirs();}
if (!imageFile.exists()){
try {imageFile.createNewFile();
} catch (IOException e) {e.printStackTrace();
}
}
// 依据文件解析出文件对应的 uri
Uri uri= FileProvider.getUriForFile(ThirdActivity.this,"com.ruan.testw.my_provider",imageFile);
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
// 判断是否有 Activity 能解决 intent
if (intent.resolveActivity(getPackageManager())!=null){startActivityForResult(intent,REQUEST_TAKE_PHOTO);
}
}
对应的 FileProvider 在 AndroidX 和 android 中不同
在 Androidmanifest 中配置 FileProvider
<!--com.ruan.testw.my_provider 自定义的名字 -->
<provider
android:name="androidx.core.content.FileProvider"// 内容提供者类名
android:authorities="com.ruan.testw.my_provider"// 配置一个 FileProvider 的名字,它在以后零碎内须要是惟一值。android:exported="false"// 示意该 FileProvider 是否须要公开进来,传 false 示意不公开
android:grantUriPermissions="true">// 是否容许受权文件的长期拜访权限。传 true 示意须要
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
// 在 android 包下
<!--com.ruan.testw.my_provider 自定义的名字 -->
<provider
android:name="android.support.v4.content.FileProvider"// 内容提供者类名
android:authorities="com.ruan.testw.my_provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
在 Android 的 res 下创立 xml 文件夹下新建 file_paths.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 代表内部存储区域的根目录下的文件 Environment.getExternalStorageDirectory()/DCIM/camerademo 目录 -->
<external-path name="hm_DCIM" path="DCIM/camerademo" />
<!-- 代表内部存储区域的根目录下的文件 Environment.getExternalStorageDirectory()/Pictures/camerademo 目录 -->
<external-path name="hm_Pictures" path="Pictures/camerademo" />
<!-- 代表 app 公有的存储区域 Context.getFilesDir()目录下的 images 目录 /data/user/0/com.hm.camerademo/files/images-->
<files-path name="hm_private_files" path="images" />
<!-- 代表 app 公有的存储区域 Context.getCacheDir()目录下的 images 目录 /data/user/0/com.hm.camerademo/cache/images-->
<cache-path name="hm_private_cache" path="images" />
<!-- 代表 app 内部存储区域根目录下的文件 Context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)目录下的 Pictures 目录 -->
<!--/storage/emulated/0/Android/data/com.hm.camerademo/files/Pictures-->
<external-files-path name="hm_external_files" path="Pictures" />
<!-- 代表 app 内部存储区域根目录下的文件 Context.getExternalCacheDir 目录下的 images 目录 -->
<!--/storage/emulated/0/Android/data/com.hm.camerademo/cache/images-->
<external-cache-path name="hm_external_cache" path="" />
</paths>
- root-path:示意根目录,“/”。new File(“/”);
- files-path:示意 content.getFileDir() 获取到的目录
- cache-path:示意 content.getCacheDir() 获取到的目录
- external-path:示意 Environment.getExternalStorageDirectory() 指向的目录
- external-files-path:示意 ContextCompat.getExternalFilesDirs() 获取到的目录
- external-cache-path:示意 ContextCompat.getExternalCacheDirs() 获取到的目录
留神:
如果 App 有抉择和剪裁图片的需要,最好配置下 root-path,这样子能够读取到 sd 卡和一些利用分身的目录,否则微信等利用分身保留的图片,在 App 外面读取时就产生上面异样:
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/forpermission/2021-03-15 16.32.35.jpg
实例:
<paths>
<!-- <root-path
name="root" path=""/>-->
<external-path
name="camera_photo"
path="forpermission"/>
</paths>
1. 应用 FileProvider.getUriForFile(ThirdActivity.this,”com.ruan.testw.my_provider”,imageFile);
目标:是将 file:// 转成 content://
2.getUriForFile() 办法,须要一个 authority 的参数,这里须要与后面在 AndroidManifest.xml 中 配置的 android:authorities 保持一致,因为是通过 android:authorities 属性配置的值,来惟一确定由谁来响应这个 provider 的。在 AndroidManifest.xml 中配置 provider 的时候,须要保障 android:authorities 的值,在整个零碎中的唯一性,否者装置的时候会抛出异样
*END:
学而不思则罔,思而不学则殆*
**