I am developing an application for Android, and part of the application has to takes pictures and save them to the SDcard. The onPictureTaken method returned a byte array with the data of the captured image.
All I need to do is save the byte array into a .jpeg image file. I have attempted to do this with the help of BitmapFactory.decodeByteArray (to get a Bitmap) and then bImage.compress (to an OutputStream), a plain OutputStream, and a BufferedOutputStream. All three of these methods seem to give me the same weird bug. My Android phone (8MP camera and a decent processor), seems to save the photo (size looks correct), but in a corrupted way (the image is sliced and each slice is shifted; or I just get almost horizontal lines of various colors); and The weird thing is, that an Android tablet with a 5MP camera and a fast processor, seems to save the image correctly.
So I thought maybe the processor can't keep up with saving large images, because I got OutOfMemory Exceptions after about 3 pictures (even at compression quality of 40). But then how does the built in Camera app do it, and much faster too? I'm pretty sure (from debug) that the OutputStream writes all the data (bytes) and it should be fine, but it's still corrupted.
***In short, what is the best/fastest way (that works) to save a byte array to a jpeg file?
Thanks in advance, Mark
code I've tried (and some other slight variations):
try { Bitmap image = BitmapFactory.decodeByteArray(args, 0, args.length); OutputStream fOut = new FileOutputStream(externalStorageFile); long time = System.currentTimeMillis(); image.compress(Bitmap.CompressFormat.JPEG, jpegQuality, fOut); System.out.println(System.currentTimeMillis() - time); fOut.flush(); fOut.close(); } catch (Exception e) { }
and
try { externalStorageFile.createNewFile(); FileOutputStream fos = new FileOutputStream(externalStorageFile); fos.write(args); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); }
Convert byte[] array to File using Java In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementation: Convert a String into a byte array and write it in a 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);
The onPictureTaken method returned a byte array with the data of the captured image. All I need to do is save the byte array into a .jpeg image file. I have attempted to do this with the help of BitmapFactory.decodeByteArray (to get a Bitmap) and then bImage.compress (to an OutputStream), a plain OutputStream, and a BufferedOutputStream.
A byte array is just an array of bytes. An image is essentially a file. So the task is converting the file to an array so that it can be stored or transferred more easily in different kinds of applications. 1. Method 1
First of all, the byte type in Java is an 8-bit signed two's complement integer. Its range is [-128, 127]. A byte array is just an array of bytes. An image is essentially a file. So the task is converting the file to an array so that it can be stored or transferred more easily in different kinds of applications.
An image is essentially a file. So the task is converting the file to an array so that it can be stored or transferred more easily in different kinds of applications. 1.
All I need to do is save the byte array into a .jpeg image file.
Just write it out to a file. It already is in JPEG format. Here is a sample application demonstrating this. Here is the key piece of code:
class SavePhotoTask extends AsyncTask<byte[], String, String> { @Override protected String doInBackground(byte[]... jpeg) { File photo=new File(Environment.getExternalStorageDirectory(), "photo.jpg"); if (photo.exists()) { photo.delete(); } try { FileOutputStream fos=new FileOutputStream(photo.getPath()); fos.write(jpeg[0]); fos.close(); } catch (java.io.IOException e) { Log.e("PictureDemo", "Exception in photoCallback", e); } return(null); } }
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