android 11以上和以下保存图片
时间: 2023-12-21 13:05:10 浏览: 33
在 Android 11 及以上版本中,由于存储权限的限制,使用传统的方式保存图片会比较困难。建议使用 MediaStore API 来保存图片,具体步骤如下:
1. 请求存储权限:
```java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
} else if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
```
2. 使用 MediaStore API 保存图片:
```java
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, "image.jpg");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/myapp");
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
OutputStream outputStream = getContentResolver().openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.close();
Toast.makeText(MainActivity.this, "图片保存成功", Toast.LENGTH_SHORT).show();
```
其中,使用 ContentValues 设置图片的名称、MIME 类型和相对路径,然后调用 getContentResolver().insert() 方法将图片保存到 MediaStore 中,最后使用 getContentResolver().openOutputStream() 获取输出流,将 Bitmap 写入输出流中。
在 Android 11 以下版本中,可以使用传统的方式保存图片,具体步骤如下:
```java
String fileName = "image.jpg";
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, fileName);
OutputStream outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.close();
Toast.makeText(MainActivity.this, "图片保存成功", Toast.LENGTH_SHORT).show();
```
其中,使用 Environment.getExternalStoragePublicDirectory() 获取图片保存目录,然后创建文件,并将 Bitmap 写入文件中。需要注意的是,保存图片需要 WRITE_EXTERNAL_STORAGE 权限。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)