Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a byte array as jpg file in android device folder

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.

like image 637
Manoj Pathak Avatar asked Sep 07 '17 18:09

Manoj Pathak


People also ask

How do I save a byte array as a picture?

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.

How do I save a byte file?

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

How to Save a byte array to a file in c#?

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.

What is byte array in Android?

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() .


1 Answers

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);
}
like image 192
SushiHangover Avatar answered Sep 30 '22 10:09

SushiHangover