在android设备文件夹中将字节数组保存为jpg文件 [英] Saving a byte array as jpg file in android device folder
问题描述
我正在尝试将图像保存在 Android 设备的文件夹中.我使用的代码如下
I am trying to save an image in a folder of the Android device. The code I am using is as follows
var newFolder = AndroidEnvironment.GetExternalStoragePublicDirectory(AndroidEnvironment.DirectoryPictures).AbsolutePath + "/NewFolder";
Directory.CreateDirectory(cameraFolder);
byte[] reducedImage = ResizeImageAndroid(imageData, 50, 50, 70);
Image image = new Image {Source = ImageSource.FromStream(() => new MemoryStream(reducedImage))};
我想将缩小的图像保存为newFolder"中的 jpg 文件.我不确定我的方向是否正确,如果我将 reducedImage 或 image 保存为 newFolder 中的 jpg 文件会很棒.我正在使用 Xamarin,此代码在 Android 项目中.
I want to save reduced image as jpg file in "newFolder". I am not sure if I am in right direction, it would be great if I save reducedImage or image as a jpg file in newFolder. I am using Xamarin and this code is in Android project.
我已经检查过 this 帖子,我不明白那里发生了什么.
I already checked this post and I don't understand whats happening there.
推荐答案
使用 Directory.CreateDirectory
在公共 Picture
目录和 FileOutputStream<中创建您的文件夹/code> 将您的字节 [] 写入文件.
Use Directory.CreateDirectory
to create your folder in the public Picture
directory and FileOutputStream
to write your byte[] to a file.
byte[] reducedImage = `some jpeg-based byte array`
var filename = System.IO.Path.Combine(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures).ToString(), "NewFolder");
Directory.CreateDirectory(filename);
filename = System.IO.Path.Combine(filename, "filename.jpg");
using (var fileOutputStream = new FileOutputStream(filename))
{
await fileOutputStream.WriteAsync(reducedImage);
}
这篇关于在android设备文件夹中将字节数组保存为jpg文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!