fragment中动态加载存储权限完整代码
时间: 2023-10-06 15:14:08 浏览: 39
以下是一个示例代码,用于在 Fragment 中请求和检查存储权限,并在权限被授予时动态加载文件:
```
public class MyFragment extends Fragment {
private static final int REQUEST_PERMISSIONS = 1;
private boolean hasStoragePermission = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkStoragePermission();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_my, container, false);
// Load file if storage permission is granted
if (hasStoragePermission) {
loadFile();
}
return view;
}
private void checkStoragePermission() {
if (ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// Permission is not granted, request it
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_PERMISSIONS);
} else {
// Permission is already granted
hasStoragePermission = true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_PERMISSIONS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission is granted
hasStoragePermission = true;
loadFile();
} else {
// Permission is denied
Toast.makeText(getContext(), "Storage permission is required!", Toast.LENGTH_SHORT).show();
}
}
}
private void loadFile() {
// Load file
File file = new File(Environment.getExternalStorageDirectory(), "my_file.txt");
// Do something with the file
// ...
}
}
```
在 `onCreate()` 方法中,调用 `checkStoragePermission()` 方法检查存储权限。如果权限已经被授予,变量 `hasStoragePermission` 将设置为 `true`。如果没有授予权限,则使用 `requestPermissions()` 方法请求该权限。
当用户响应权限请求时,系统会调用 `onRequestPermissionsResult()` 方法。如果权限被授予,变量 `hasStoragePermission` 将设置为 `true`,然后可以调用 `loadFile()` 方法加载文件。
在 `onCreateView()` 方法中,如果存储权限已经被授予,将加载文件。否则,文件将在权限被授予后加载。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)