I'm uploading JPG image as byte[] but Bitmap strips of EXIF before converting to byte[]. How do I upload raw jpg without converting it to Bitmap?
File imagefile = new File(filepath + "DSC00021.jpg");
FileInputStream fis = new FileInputStream(imagefile);
Bitmap bi = BitmapFactory.decodeStream(fis); // EXIF info lost
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPG, 100, baos);
byte[] data = baos.toByteArray();
p.s. I don't want to use any 3rd party library. ExifInterface can only write to file and not streams/byte arrays.
Above code will not work most cases. In case if you want to decode large size image you will get "out of memory error". Decode using bitmpafactory options.
Convert the file to bitmap by
Bitmap bi = BitmapFactory.decode(filepath + "DSC00021.jpg");
You can specify options too, look at API documentation
Or if you want to exchange the meta data from one file to another, sanselan will probably be the best choice. This would be much helpful when you manipulating the image, for example re-size.
The sample code will guide you in a right direction.
Related
In general vector drawables are compiled, and accessed with their ids in execution time.
My project consist of pulling out images from a server. For now, i'm doing it for png files, but i would try it with vectors ".xml".
I'm able to download those, but i can't parse them in drawable type to show them in views.
How can i do this with a bunch of bytes representing the vectors resources and parsing them to drawables ?
It would be a huge thing for my app since it will be much much lighter, and good for those pixelized pictures.
The inflate method may be helpful, but you could use a third party library:androidsvg. Don't reproduce the wheel if possible.
If you can access the image from the server via url, use Glide to load it to your View. By so doing, you don't have to download the image to your storage before using it in an ImageView. However, if you still want to get the image as a byte array and display it in a view, use the code snippet below...
Bitmap bm = BitmapFactory.decodeFile("/path/to/yourimage.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray(); //This is the image byte
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT); //Encoded string
Furthermore, you can convert the string to a Bitmap and then pass it to your ImageView.
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap bmp= BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
image.setImageBitmap(bmp);
Let me know if you have any question concerning this, I'd be glad to help.
I am trying to use the Azure Face API on android. I am capturing an image from the device camera and then converting it to an InputStream to be sent to the detect method. I keep getting the error "com.microsoft.projectoxford.face.rest.ClientException: Image size is too small"
I checked the documentation and the image size is 1.4Mb which is within the 1Kb-4Mb range. I don't understand why it isn't working.
Bitmap bitmap = cameraKitImage.getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
bitmapdata = bos.toByteArray();
new FaceTask().execute(new ByteArrayInputStream(bitmapdata));
Face[] faces = faceServiceClient.detect(inputStreams[0], true, false, null);
Somehow your file is being compressed more than 1Kb or if it is small in actual size already. Try to save it somewhere in the drawable or assets folder and open it in the input stream.
I need to give the user possibility to share his high score. I took an image (PNG) and wrote the high score on it on it. Can I convert BitMap to PNG and share it without saving the file?
You can use the boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) of the Bitmap class.
ByteArrayOutputStram baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] png = baos.toByteArray();
You will have the byte[] contained the image in png format. You can then use any API you want to share that image.
Is there any way to store image files in firebase using Java api to be used for android application?
I have read this thread Can I store image files in firebase using Java API and still there is no answer. I know that there is an official api for it, called firepano here is the link https://github.com/firebase/firepano but it is for Javascript library.
is there any solution for Java library??
I used this code and now it's working:
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.chicken);//your image
ByteArrayOutputStream bYtE = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bYtE);
bmp.recycle();
byte[] byteArray = bYtE.toByteArray();
String imageFile = Base64.encodeToString(byteArray, Base64.DEFAULT);
I want to upload an image to my website via my android app so for that i want to convert my image to Byte[].
i have used the following code but not work for me..
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG,0, bos);
byte[] data=bos.toByteArray();
So please share with me any other way to convert an Image to Byte[]..
Use ByteBuffer:
array = new byte[w*h*4];
Buffer dst = ByteBuffer.wrap(array);
bmp.copyPixelsToBuffer(dst);
and use array the way you want...