【发布时间】:2018-03-15 22:26:51
【问题描述】:
我的应用程序用于从用户获取文本(字符串)并将其保存在内部和/或外部存储中,从中读取,问题开始于大约两个日期,当时我尝试在使用的虚拟设备上运行应用程序OS android 7.0.1“Nougat”和应用程序跳过/忽略写入外部存储权限,即使我看到权限对话框在我点击外部缓存按钮时出现
我的问题是:这是否意味着android OS android marshmallow 及以上版本不需要创建权限方法?
看这个 GIF 来联系
整个代码
public class MainFragment extends android.app.Fragment {
private View view;
private EditText userName2;
private Button internalButton, externalButton, showData;
FileOutputStream fileOutputStream;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_main, container, false);
userName2 = view.findViewById(R.id.editTextUserName);
internalButton = view.findViewById(R.id.internalButton);
externalButton = view.findViewById(R.id.externalButton);
showData = view.findViewById(R.id.showData);
internalButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveToInternalCache();
}
});
externalButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
enableRunTimePermisstion();
}
});
showData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), DataActivity.class);
startActivity(intent);
}
});
return view;
}
@TargetApi(23)
public void enableRunTimePermisstion() {
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) &&
(getContext().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED))
if (getActivity().shouldShowRequestPermissionRationale
(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(getActivity(), "Write storage permission is need for app"
, Toast.LENGTH_LONG).show();
} else {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
Toast.makeText(getActivity(), "request permission"
, Toast.LENGTH_LONG).show();
}
saveToExternalCache();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE) &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getActivity(), "Write external storage granted", Toast.LENGTH_LONG).show();
saveToExternalCache();
}
} else {
Toast.makeText(getActivity(), "Write external permission denied", Toast.LENGTH_SHORT).show();
enableRunTimePermisstion();
}
}
public void saveToInternalCache() {
String userName = userName2.getText().toString();
File path = getActivity().getCacheDir();
File file = new File(path, "dataInternal.txt");
try {
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(userName.getBytes());
Toast.makeText(getActivity(), "Data stored successfully" + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.e("IOException", e.toString());
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
Log.e("IOException", e.toString());
}
}
}
public void saveToExternalCache() {
String userName = userName2.getText().toString();
File path = getActivity().getExternalCacheDir();
File file = new File(path, "dataExternal.txt");
try {
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(userName.getBytes());
Toast.makeText(getActivity(), "Data stored successfully"
+ file.getAbsolutePath(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.e("IOException", e.toString());
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
Log.e("IOException", e.toString());
}
}
}
}
【问题讨论】:
标签: android android-fragments android-permissions android-6.0-marshmallow android-external-storage