保存图片到相册

项目中有个需求是将图片保存到相册中,本文整理了该功能如下,记录方便以后使用

String fileName = null;
            //系统相册目录
            String galleryPath= Environment.getExternalStorageDirectory()
                  + File.separator + Environment.DIRECTORY_DCIM
                  +File.separator+"Camera"+File.separator;
            //Drawable转换成bitmap
               GlideBitmapDrawable bd = (GlideBitmapDrawable) drawable;
              Bitmap bitmap  = bd.getBitmap();
            


            // 声明文件对象
            File file = null;
            // 声明输出流
            FileOutputStream outStream = null;

            try {
               // 如果有目标文件,直接获得文件对象,否则创建一个以filename为名称的文件
               String temp = UUID.randomUUID().toString();
               file = new File(galleryPath, temp+ ".jpg");

               // 获得文件相对路径
               fileName = file.toString();
               // 获得输出流,如果文件中有内容,追加内容
               outStream = new FileOutputStream(fileName);
               if (null != outStream) {
                  bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outStream);
               }

            } catch (Exception e) {
               e.getStackTrace();
            }finally {
               try {
                  if (outStream != null) {
                     outStream.close();
                  }
               } catch (IOException e) {
                  e.printStackTrace();
               }
 }
            MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), bitmap, fileName, null);
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(file);
            intent.setData(uri);
            activity.sendBroadcast(intent);

### NativeGallery 保存图片相册的使用方法 在 Unity 中,`NativeGallery` 插件提供了简单易用的接口来实现将图片保存相册的功能。以下是具体的使用方法和示例代码。 #### 示例代码 以下是一个完整的示例,展示如何将一张 `Texture2D` 格式的图片保存相册中[^1]。 ```csharp using UnityEngine; using NativeGallery; public class SaveImageExample : MonoBehaviour { void Start() { // 创建一个 Texture2D 对象 Texture2D pic = new Texture2D(512, 512); Color[] colors = new Color[512 * 512]; // 填充颜色数据(例如纯红色) for (int i = 0; i < colors.Length; i++) { colors[i] = Color.red; } pic.SetPixels(colors); pic.Apply(); // 将 Texture2D 编码为 PNG 格式 byte[] bytes = pic.EncodeToPNG(); // 使用 NativeGallery 保存图片相册 NativeGallery.SaveImageToGallery(bytes, "MyFolder", "example.png", (string error) => { if (!string.IsNullOrEmpty(error)) { Debug.LogError("保存图片失败: " + error); } else { Debug.Log("图片已成功保存相册!"); } }); } } ``` #### 参数说明 - `bytes`: 图片的字节数组,可以通过 `EncodeToPNG()` 或 `EncodeToJPG()` 方法从 `Texture2D` 获取。 - `albumName`: 指定保存图片相册名称。如果该相册不存在,系统会自动创建。 - `imageName`: 保存后的图片文件名。 - `callback`: 回调函数,用于处理保存结果。如果保存失败,`error` 参数会包含错误信息;否则为空字符串。 #### 注意事项 - 在 Android 平台上,需要确保应用具有存储权限。可以在 Unity 的 `AndroidManifest.xml` 文件中添加以下权限声明[^3]: ```xml <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` - 在 iOS 平台上,必须在 `Info.plist` 文件中添加以下键值对以请求访问相册的权限: ```xml <key>NSPhotoLibraryAddUsageDescription</key> <string>我们需要访问您的相册保存图片。</string> ``` #### 其他功能 除了保存图片外,`NativeGallery` 还支持保存视频到相册、加载图片以及获取相册中的所有图片或视频列表等功能[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值