问题
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))};
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.
I already checked this post and I don't understand whats happening there.
回答1:
Use Directory.CreateDirectory to create your folder in the public Picture directory and FileOutputStream to write your byte[] to a file.
Example:
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);
}
回答2:
I would just like to share my solution, which I found after some research and worked well.
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();
I adapted the code from the sample page and added the filestream.
来源:https://stackoverflow.com/questions/46103368/saving-a-byte-array-as-jpg-file-in-android-device-folder