Xamarin.Forms Android 将图像保存到图库 [英] Xamarin.Forms Android save image to gallery

查看:36
本文介绍了Xamarin.Forms Android 将图像保存到图库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个流,它是拍照的结果.我想将该照片保存到用户图库.

I have a stream which is the result of taking a photo. I want to save that photo to the users Gallery.

我已经尝试了各种各样的事情,但没有任何事情需要......我保存的类是一个普通的 C# 类 - 它没有从任何 android 类型继承,但如果有必要可以这样做(也许是为了激发一个意图 - 虽然我试过了,结果又出现了一个兔子洞)

I have tried all manner of things but nothing takes... The class I am saving is a plain C# class - it does not inherit form any android types but that could be done if necessary (to fire an intent perhaps - though I tried this and it ended up another rabbit hole)

我已经试过了:

try{
    using (FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + _name, FileMode.OpenOrCreate))
    {
        fs.Write(bytes, 0, bytes.Length);
    }
} catch (Exception ex) {
    Console.WriteLine (ex.Message);
}

还有:

        if (_filename.ToLower ().Contains (".jpg") || _filename.ToLower ().Contains (".png")) {
            stream.Position = 0;
            var bitmap = BitmapFactory.DecodeStream (stream);

            var finalStream = new MemoryStream ();

            bitmap.Compress (Bitmap.CompressFormat.Jpeg, 25, finalStream);
            bitmap = null;

            finalStream.Position = 0;

            var path2 = Environment.GetFolderPath (Environment.SpecialFolder.MyPictures);
            var filename2 = System.IO.Path.Combine (path2, _filename);

            using (var fileStream = File.Create (filename2)) {
                finalStream.Seek (0, SeekOrigin.Begin);
                finalStream.CopyTo (fileStream);
                fileStream.Close ();

                finalStream.Dispose ();
                stream.Dispose ();
                fileStream.Dispose ();
                GC.Collect ();
            }
            return;
        }

我认为这将是一项简单的任务!嗯...

I thought this would be an easy task! meh...

有什么帮助吗?

推荐答案

使用 MediaStore ContentProvider.MediaStore.Images.Media 有几种插入方法,您可以可用于向图库添加内容.

Use the MediaStore ContentProvider. MediaStore.Images.Media has several Insert methods you can use to add content to the gallery.

这篇关于Xamarin.Forms Android 将图像保存到图库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆