I am developing an android app in which it is required to provide permission for Read and write external storage. My requirement is to select a picture from gallery and use it in my app. Everything is working fine except Marshmallow devices. I want to provide permission for Marshmallow. Can anyone help me with this?
问题:
回答1:
It can be achieved something as following...
public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback{
private static final int REQUEST_WRITE_PERMISSION = 786;
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_WRITE_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openFilePicker();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermission();
}
private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
} else {
openFilePicker();
}
}
}
回答2:
Read through Runtime permissions - Android M and its reference project.
Permissions flow has changed requiring the permissions to be asked at runtime, preferably, as and when required. With the same.. permissions have also been categorised into Normal and Dangerous which will also lead to permission groups.
回答3:
In addition, if you want to ask for multiple permissions. You can do this.
private ArrayList<String> permisos;
private void someMethod()
{
..
verificarPermisos(Manifest.permission.READ_EXTERNAL_STORAGE );
verificarPermisos(Manifest.permission.ACCESS_FINE_LOCATION );
verificarPermisos(Manifest.permission.ACCESS_COARSE_LOCATION );
if (permisos.size()!=0)
{
solicitarPermisos(permisos.toArray(new String[permisos.size()]));
}
..
}
@TargetApi(23)
void verificarPermisos(String permiso){
if (ContextCompat.checkSelfPermission(this,permiso)
!= PackageManager.PERMISSION_GRANTED) {
permisos.add(permiso);
// Should we show an explanation?
if (shouldShowRequestPermissionRationale(permiso)) {
}
}
}
@TargetApi(23)
void solicitarPermisos(String[] permiso){
requestPermissions(permiso, 1);
}
First, I check if the permission is already granted or not with "verificarPermisos()" method. If they are not granted, I add them to "permisos" arraylist. Finally, the ungranted permissions are resquested passing them in a String[] array format.
Note that you can only request permissions since MashMellow(API 23) or higher. So, if your app is also targetting android versions before MashMellow you could use the "@TargetApi()" annotation and the permission methods only be called when the target is API 23 or higher.
Hope to help, greetings.
回答4:
Checking every situation
if denied - showing Alert dialog to user why we need permission
public static final int STORAGE_PERMISSION_REQUEST_CODE= 1;
private void askPermissions() {
int permissionCheckStorage = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
// we already asked for permisson & Permission granted, call camera intent
if (permissionCheckStorage == PackageManager.PERMISSION_GRANTED) {
//do what you want
} else {
// if storage request is denied
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("You need to give permission to access storage in order to work this feature.");
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.setPositiveButton("GIVE PERMISSION", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
// Show permission request popup
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
STORAGE_PERMISSION_REQUEST_CODE);
}
});
builder.show();
} //asking permission for first time
else {
// Show permission request popup for the first time
ActivityCompat.requestPermissions(AddWorkImageActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
STORAGE_PERMISSION_REQUEST_CODE);
}
}
}
Checking Permission Results
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case STORAGE_PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// check whether storage permission granted or not.
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//do what you want;
}
}
break;
default:
break;
}
}
you can just copy and paste this code, it works fine. change context(this) & permissions according to you.