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.
Create a ByteArrayInputStream object by passing the byte array (that is to be converted) to its constructor. Read the image using the read() method of the ImageIO class (by passing the ByteArrayInputStream objects to it as a parameter). Finally, Write the image to using the write() method of the ImageIo class.
Java – How to save byte[] to a filewrite is the simplest solution to save byte[] to a file. // bytes = byte[] Path path = Paths. get("/path/file"); Files. write(path, bytes);
Using File. The static File class in the System.IO namespace provides a simple static method WriteAllBytes() that we can use to write all the data from a byte array to a file. => File. WriteAllBytes(filePath, data); public static void SaveByteArrayToFileWithStaticMethod(byte[] data, string filePath) => File.
java.io.ByteArrayOutputStream. This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString() .
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With