亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將位圖添加到圖庫中的特定相冊?

如何將位圖添加到圖庫中的特定相冊?

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);

        };

然后將其發送到圖庫中的特定相冊。

http://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 關注
  • 129 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號