I am trying to request permission to READ_EXTERNAL_STORAGE on an Android application. My issue is that the there is no dialog displayed, thus the "onRequestPermissionsResult" is never called as well. Is there anything I am doing wrong here? I followed this link: Developer Android
Here is my code:
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
}
}
}
As you can see on the Log ActivityCompat.requestPermissions(...) is called as "REQUEST" is printed. "shouldShowRequestPermissionRationale" is never printed, regardless device I'm running. Neither "onRequestPermissionsResult" is printed.
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
I have included this in my manifest file:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Highly appreciating any help.
Updated: Is the permission requested in your manifest? You can only request permissions that are in your manifest.
Documentation here: https://developer.android.com/guide/topics/manifest/manifest-intro.html
use this class which use common class for all permission.
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;
}
}
}
}
Use this line in your mainclass where you need to check permission.
boolean result = Utility1.Utility.checkPermission(MainActivity.this);
Ensure that you have uses-permission
within manifest
but out of application
section. You can even try to ask for permission even in the should show rationale block too like:
if (ActivityCompat.shouldShowRequestPermissionRationale(AccountActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(AccountActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, F);
}
For more simplicity you can probably use some third party helper code, like one of my own: PermissionHelper
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.