在android设备文件夹中将字节数组保存为jpg文件

时间:2017-09-07 18:59:11

标签: android xamarin xamarin.android

我正在尝试将图片保存在Android设备的文件夹中。我使用的代码如下

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 图片保存为 newFolder 中的jpg文件,那就太棒了。我正在使用Xamarin,此代码在Android项目中。

我已经检查了this帖子,我不明白那里发生了什么。

2 个答案:

答案 0 :(得分:4)

使用Directory.CreateDirectory在公共Picture目录中创建文件夹,并使用FileOutputStream将字节[]写入文件。

实施例

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

答案 1 :(得分:0)

我想分享我的解决方案,我在经过一些研究后发现并且运作良好。

 var btnSave = FindViewById<Button>(Resource.Id.btnSave);
            btnSave.Click += async delegate
                {
                    if (signature.IsBlank)
                    {
                        // display the base line for the user to sign on.
                        Android.Support.V7.App.AlertDialog.Builder alert = new Android.Support.V7.App.AlertDialog.Builder(this);
                        alert.SetMessage(Resources.GetString(Resource.String.signature_isempty));
                        alert.SetNeutralButton("OK", delegate { });
                        alert.Create().Show();
                    }
                    else
                    {
                        points = signature.Points;
                        try
                        {

                            string nString = String.Format("mySign_{0}.png", Guid.NewGuid());
                            string absolutePath = String.Format("{0}/{1}", Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).ToString(), GetString(Resource.String.app_name).ToString());
                            var filePath = System.IO.Path.Combine(absolutePath, nString);

                            /**

                             You can get base64 and save your database
                            **/
                            /*var img = await signature.GetImageStreamAsync(SignatureImageFormat.Jpeg);
                            var signatureMemoryStream = (MemoryStream)img;
                            byte[] data = signatureMemoryStream.ToArray();
                            string base64img = Convert.ToBase64String(data);
                            Log.Info(NativeConfig.TAG_DROID, base64img.ToString());

                            File.WriteAllBytes(filePath, data);*/

                            var img = await signature.GetImageStreamAsync(SignatureImageFormat.Png, Color.Black, Color.White, true);
                            var ms = (MemoryStream)img;
                            using (FileStream file = new FileStream(filePath, FileMode.Create, System.IO.FileAccess.Write))
                            {
                                byte[] bytes = new byte[ms.Length];
                                ms.Read(bytes, 0, (int)ms.Length);
                                file.Write(bytes, 0, bytes.Length);
                                ms.Close();
                            }

                        }
                        catch (Exception e)
                        {
                            Log.Info(NativeConfig.TAG_DROID, e.Message);
                            Toast.MakeText(this, "No se pudo guardar", ToastLength.Short).Show();
                        }
                    }
                };
            btnSave.Dispose();

我调整了示例页面中的代码并添加了filestream