我正在尝试在Android应用程序上请求READ_EXTERNAL_STORAGE的权限。我的问题是没有显示对话框,因此也不会调用“onRequestPermissionsResult”。我在这里做错了吗?我点了这个链接:Developer Android
这是我的代码:
public class AccountActivity extends AppCompatActivity {
protected TextView changeProfileImgBtn;
private static final int SELECTED_PICTURE = 1;
private static final int F = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account);
changeProfileImgBtn = (TextView) findViewById(R.id.accountChangeProfileImgBtn);
changeProfileImgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(AccountActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(AccountActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
System.out.println("shouldShowRequestPermissionRationale");
} else {
ActivityCompat.requestPermissions(AccountActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
F);
System.out.println("REQUEST");
}
} else {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECTED_PICTURE);
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[], @NonNull int[] grantResults) {
System.out.println("onRequestPermissionsResult");
switch (requestCode) {
case F: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
System.out.println("GRANTED");
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECTED_PICTURE);
} else {
System.out.println("DENIED");
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
}
正如您在Log ActivityCompat.requestPermissions(...)上看到的那样,打印出“REQUEST”。永远不会打印“shouldShowRequestPermissionRationale”,无论我正在运行什么设备。 “onRequestPermissionsResult”都没有打印出来。
D/ViewRootImpl: ViewPostImeInputStage processPointer 0
D/ViewRootImpl: ViewPostImeInputStage processPointer 1
I/System.out: REQUEST
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@ee9f27b time:106175669
我已将其包含在我的清单文件中:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
非常感谢任何帮助。
答案 0 :(得分:0)
已更新:您的清单中是否已请求权限?您只能请求清单中的权限。
此处的文档:https://developer.android.com/guide/topics/manifest/manifest-intro.html
答案 1 :(得分:0)
使用此类使用公共类进行所有权限。
public class Utility1 {
public static class Utility {
public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static boolean checkPermission(final Context context)
{
int currentAPIVersion = Build.VERSION.SDK_INT;
if(currentAPIVersion>=android.os.Build.VERSION_CODES.M)
{
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Permission necessary");
alertBuilder.setMessage("External storage permission is necessary");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
return false;
} else {
return true;
}
} else {
return true;
}
}
}
}
在您需要检查权限的主类中使用此行。
boolean result = Utility1.Utility.checkPermission(MainActivity.this);
答案 2 :(得分:0)
确保uses-permission
位于manifest
但application
部分之外。您甚至可以尝试在“应该显示基本原理”块中请求许可:
if (ActivityCompat.shouldShowRequestPermissionRationale(AccountActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(AccountActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, F);
}
为了更简单,您可以使用一些第三方帮助程序代码,例如我自己的代码:PermissionHelper