這是我到目前爲止所做的,但問題是,代碼從不要求權限,沒有錯誤,沒有強制關閉。我目前正在測試Android 6.0設備。我甚至在設置中撤銷了權限,但它仍然沒有問。我已經發布了完整的代碼如下─如何申請android 6.0的存儲權限?
package com.example.pper;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
public class MainActivity extends Activity {
private String TAG;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
isStoragePermissionGranted();
}
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Log.v(TAG, "Permission is granted");
return true;
} else {
Log.v(TAG, "Permission is revoked");
ActivityCompat
.requestPermissions(
this,
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
1);
return false;
}
} else { // permission is automatically granted on sdk<23 upon
// installation
Log.v(TAG, "Permission is granted");
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.v(TAG, "Permission: " + permissions[0] + "was "
+ grantResults[0]);
// resume tasks needing this permission
}
}
}
我卸載應用程序,並重新安裝了 –
你嘗試調試/日誌,看看問題出在哪裏? –