Xamarin.Forms Android将图像保存到图库 [英] Xamarin.Forms Android save image to gallery
问题描述
我有一条流,这是拍照的结果.我想将该照片保存到用户图库.
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屋!