Android: write PNG ByteArray to file

独自空忆成欢 提交于 2019-12-24 00:56:30

问题


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.

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!!!


回答1:


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


来源:https://stackoverflow.com/questions/24966061/android-write-png-bytearray-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!