我有这个应用程序,它将图像选择到画廊,并使用Imageview将其显示给测试。我的问题是它不能在Android M上运行。我可以选择图像但不能在测试中显示。他们说我需要请求访问android M上图像的权限,但不知道如何操作。请帮忙。
最佳答案
从Android 6.0(API级别23)开始,用户在应用程序运行时(而不是在安装应用程序时)授予应用程序权限。
类型1- 当您的应用请求权限时,系统会向用户显示一个对话框。当用户响应时,系统会调用您应用的onRequestPermissionsResult()方法,并将用户响应传递给该方法。您的应用必须重写该方法才能确定是否已授予权限。回调传递给您的请求代码与传递给requestPermissions()的请求代码相同。
private static final int PICK_FROM_GALLERY = 1;
ChoosePhoto.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View v){
try {
if (ActivityCompat.checkSelfPermission(EditProfileActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(EditProfileActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PICK_FROM_GALLERY);
} else {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults)
{
switch (requestCode) {
case PICK_FROM_GALLERY:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
} else {
//do something like displaying a message that he didn`t allow the app to access gallery and you wont be able to let him select from gallery
}
break;
}
}
类型2- 如果您想在一个地方背靠背授予运行时权限,则可以点击以下链接Android 6.0 multiple permissions
并在 list 中添加您的要求的权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
注-如果Manifest.permission.READ_EXTERNAL_STORAGE产生错误,则将其替换为android.Manifest.permission.READ_EXTERNAL_STORAGE。==> 如果您想了解更多有关运行时权限的信息,请点击以下链接
https://developer.android.com/training/permissions/requesting.html
-----------------------------更新1 ------------------ --------------
使用EasyPermissions的运行时权限
EasyPermissions是一个包装器库,用于简化针对Android M或更高版本的基本系统权限逻辑。
安装
在应用程序级别gradle中添加依赖项
dependencies {
// For developers using AndroidX in their applications
implementation 'pub.devrel:easypermissions:3.0.0'
// For developers using the Android Support Library
implementation 'pub.devrel:easypermissions:2.0.1'
}
您的 Activity (或片段)将覆盖onRequestPermissionsResult方法: @Override
public void onRequestPermissionsResult(int requestCode, String[]
permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,grantResults);
// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
请求权限private static final int LOCATION_REQUEST = 222;
调用此方法@AfterPermissionGranted(LOCATION_REQUEST)
private void checkLocationRequest() {
String[] perms = {Manifest.permission.ACCESS_FINE_LOCATION};
if (EasyPermissions.hasPermissions(this, perms)) {
// Already have permission, do the thing
// ...
} else {
// Do not have permissions, request them now
EasyPermissions.requestPermissions(this,"Please grant permission",
LOCATION_REQUEST, perms);
}
}
(可选)为得到更好的控制,您可以让Activity/Fragment实现PermissionCallbacks接口(interface)。实现EasyPermissions.PermissionCallbacks
@Override
public void onPermissionsGranted(int requestCode, List<String> list) {
// Some permissions have been granted
// ...
}
@Override
public void onPermissionsDenied(int requestCode, List<String> list) {
// Some permissions have been denied
// ...
}
链接-> https://github.com/googlesamples/easypermissions-----------------------------更新2适用于KOTLIN ---------------- ----------------
使用florent37的运行时权限
安装在应用程序级别gradle中添加依赖项
依赖性
implementation 'com.github.florent37:runtime-permission-kotlin:1.1.2'
在代码中 askPermission(
Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
) {
// camera or gallery or TODO
}.onDeclined { e ->
if (e.hasDenied()) {
AlertDialog.Builder(this)
.setMessage(getString(R.string.grant_permission))
.setPositiveButton(getString(R.string.yes)) { dialog, which ->
e.askAgain()
} //ask again
.setNegativeButton(getString(R.string.no)) { dialog, which ->
dialog.dismiss()
}
.show()
}
if (e.hasForeverDenied()) {
e.goToSettings()
}
}
链接-> https://github.com/florent37/RuntimePermission