将照片保存到 Android 个图库

Save photos to Android Gallery

我尝试将用 phone 拍摄的 Bitmap 照片添加到 Android Studio 的相册中,但出现错误:

Failed to insert image
   java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=4110, uid=10058 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()

我在Manifest中使用过xml:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="21"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="21" />

我保存图片的方式是:

MediaStore.Images.Media.insertImage(getContentResolver(), FullBitmap, "Name" , "Date");

我正在使用 Nexus 5 模拟器和 Android 5.1 版。

正如@njzk2 指出的那样,不要使用 android:maxSdkVersion 属性。此属性用于更高 API 级别不再需要的权限。文档中给出的示例是您应该应用此属性的少数原因之一:

For example, beginning with Android 4.4 (API level 19), it's no longer necessary for your app to request the WRITE_EXTERNAL_STORAGE permission when your app wants to write to its own application-specific directories on external storage (the directories provided by getExternalFilesDir()). However, the permission is required for API level 18 and lower.

在您的例子中,您正在访问 public 目录;为此,您需要WRITE_EXTERNAL_STORAGE所有API级别的许可。