讀取外部存儲許可在onCreate方法在版本的Android M錯誤
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSION_REQUEST_CODE);
}
在onRequestPermissionsResult
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[],int[]grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
String path = Environment.getExternalStorageState();
} else {
}
return;
}
}
我已經努力嘗試但都崩潰的應用程序結束。權限不被授予應用程序。爲什麼?我做錯了什麼?您的幫助將不勝感激。
你能從日誌發佈異常時崩潰發生? –
AndroidRuntime:致命例外:主要 進程:com.google.android.packageinstaller,PID:14436 java.lang.RuntimeException:無法啓動活動ComponentInfo {com.google.android.packageinstaller/com.android.packageinstaller.permission。 ui.GrantPermissionsActivity}:java.lang.NullPointerException:嘗試獲取空數組的長度 at android.app.ActivityThread.performLaunchActivity – Abdullah