从 Xamarin Android 文档中的字节数组创建图像文件

编程入门 行业动态 更新时间:2024-10-15 04:23:29
本文介绍了从 Xamarin Android 文档中的字节数组创建图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

有人可以帮助我了解如何从 Xamarin Android 文档中的字节数组创建图像文件并获取图像的新路径吗?

Could anyone help me about how to create image file from byte array in documents Xamarin Android and get the new path for the image please ?

这是我的代码:

Stream stream = ContentResolver.OpenInputStream(data.Data);
Bitmap bitmap = BitmapFactory.DecodeStream(stream);
MemoryStream memStream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Jpeg, 50, memStream);
byte[] picData;
picData = memStream.ToArray();

现在 picData 是字节数组,我需要在文档中创建一个 Jpeg 文件并获取新路径..提前谢谢.

now picData is byte array, and I need to create a Jpeg file in doucments and get the new path .. Thanks advance.

推荐答案

您可以使用 MemoryStream 绕过并将 Android Bitmap 直接解码/压缩为 FileStream 节省资源(内存和处理时间):

You can bypass using a MemoryStream and decode/compress an Android Bitmap directly to a FileStream to save resources (memory and processing time):

var bitmap = BitmapFactory.BitmapFactory.DecodeStream(stream);
var path = Path.Combine(GetExternalFilesDir(Environment.DirectoryDocuments).AbsolutePath, "sameImagePath.jpg");
if (!File.Exists(path))
{
    using (var filestream = new FileStream(path, FileMode.Create))
    {
        if (bitmap.Compress(Bitmap.CompressFormat.Jpeg, 50, filestream))
        {
            filestream.Flush();
        }
        else {} // handle failure case...
    }
}
bitmap.Recycle();
bitmap.Dispose();

这篇关于从 Xamarin Android 文档中的字节数组创建图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-24 18:36:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1068440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字节   图像文件   文档   Xamarin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!