为了账号安全,请及时绑定邮箱和手机立即绑定

如何将位图添加到图库中的特定相册?

如何将位图添加到图库中的特定相册?

C#
慕标琳琳 2022-12-24 12:18:07
我尝试了很多方法,但都没有用。我想创建一个特定的相册并将位图保存到其中的 JPEG 中。我使用 Xamarin。我该怎么办?
查看完整描述

2 回答

?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

我写了一个关于它的演示。


我将图片放在资源文件夹中,然后将其读取为位图


 btnSelectImage = (Button)FindViewById(Resource.Id.btnSelectImage);

        Bitmap bitmap = BitmapFactory.DecodeResource(this.ApplicationContext.Resources , Resource.Drawable.splashlogo);

        btnSelectImage.Click += (o, e) =>

        {

            saveImageToGally(bitmap, this);

        };

然后将其发送到图库中的特定相册。

//img1.sycdn.imooc.com//63a67d980001eaa304540840.jpg

有代码。


public void saveImageToGally(Bitmap finalBitmap,Context context)

    {

        //create a directory called MyCamera

        string root = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDcim).ToString() + "/MyCamera/";


       //create the Directory

        System.IO.Directory.CreateDirectory(root);


        File myDir = new File(root);

        myDir.Mkdir();

        //Image name

        string fname = "Image-" + "Image1" + ".png";

        File file = new File(myDir, fname);

        Log.Error("FilePath", file.AbsolutePath);


        if (file.Exists()) file.Delete();

        Log.Error("FilePath", root + fname);

        //total path

        string path = root + fname;


        try

        {



            var fs = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate);

            if (fs != null)

            {

                finalBitmap.Compress(Bitmap.CompressFormat.Png, 90, fs);

                fs.Close();

            }


        }

        catch (Exception e)

        {

            e.PrintStackTrace();


        }


        MediaScannerConnection.ScanFile(context, new string[] { file.Path }, new string[] { "image/jpeg" }, null);


    }

请不要忘记添加以下权限。


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

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


查看完整回答
反对 回复 2022-12-24
?
繁星淼淼

TA贡献1775条经验 获得超11个赞

请注意,您应该在运行时授予外部存储权限,因为它们是关键权限!

您可以在此处阅读更多内容。


查看完整回答
反对 回复 2022-12-24
  • 2 回答
  • 0 关注
  • 84 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信