安卓:写PNG的ByteArray到文件

安卓:写PNG的ByteArray到文件

问题描述:

我看过一个图像文件到字节数组,但我怎么能写回来。我的意思的ByteArray保存到图像文件中的文件系统。 PNG格式preferred。

I have read an image file into ByteArray, but how can I write it back. I mean save ByteArray to image file in the file system. PNG format preferred.

我的code从PNG文件的ByteArray:

My code from PNG file to ByteArray:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), mUri);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

我知道有一些类似的问题,但我没有找到这一个确切的解决方案。谢谢!

I know there are some similar questions, but I didn't find the exact solution for this one. Thanks!!!

只需使用的FileOutputStream 来写你的字节数组。像这样的:

Just use a FileOutputStream to write your byte array into. Like this:

File file = new File(getFilesDir()+"/file.png");
FileOutputStream fos = new FileOutputStream(file);

//write your byteArray here
fos.write(byteArray);
fos.flush();
fos.close();