由于Android6.0有着更高的隐私权,所以在存储SD卡要动态申请权限
if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED){
download();
}else {
//申请权限
ActivityCompat.requestPermissions(UpgradeActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUESTCODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode==REQUESTCODE){
if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
download();
}else {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setMessage("当前应用没有存储权限,不能使用分享功能,请前往设置");
builder.setNegativeButton("知道了", null);
builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startAppSettings();
}
});
builder.setCancelable(false);
builder.show();
}
}
}
/**
* 启动应用的设置 来手动开启权限
*/
private void startAppSettings() {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}