问题
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