我正在尝试获取READ_EXTERNAL_STORAGE权限。这是我的代码
// Check if user granted the permission
int hasReadExternalStoragePermission = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE);
if(hasReadExternalStoragePermission == PackageManager.PERMISSION_GRANTED) {
displaySongs();
} else {
// Should we show explanation?
if( ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Show explanation to user
} else {
// No explanation needed. We can request permissions
String permissions[] = {
Manifest.permission.READ_EXTERNAL_STORAGE
};
ActivityCompat.requestPermissions(this, permissions, REQUEST_PERMISSIONS_CODE);
}
}
这是回叫方法
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_PERMISSIONS_CODE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
displaySongs();
} else {
// Permission Denied
Toast.makeText(MainActivity.this, "Permissions denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
这是我在Android监视器中收到的错误消息
09-27 20:05:48.143 31106-31106/org.javacad.music E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.javacad.music, PID: 31106
java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=123, result=0, data=null} to activity {org.javacad.music/org.javacad.music.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at org.javacad.music.MainActivity.onRequestPermissionsResult(MainActivity.java:129)
at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6582)
at android.app.Activity.dispatchActivityResult(Activity.java:6460)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我已经将权限定义为AndoridManifest.xml
中manifest元素的直接子元素 <!--READ_EXTERNAL_PERMISSION to read files form storage-->
<user-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
答案 0 :(得分:6)
在我的情况下,由于我试图在onRequestPermissionsResult
内立即显示片段,因此发生了此异常。添加延迟修复了问题。
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case CAMERA_REQ_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
showFragment(QRCodeFragment.newInstance()); //Fragment transaction code
}
}, 500);
} else {
Toast.makeText(this, "Please grant camera permission to use the QR Scanner", Toast.LENGTH_SHORT).show();
}
}
}