2

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.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Taranfx
  • 10,361
  • 17
  • 77
  • 95
  • 2
    Why don't you read the entire file contents from the FileInputStream to byte array rather than converting to Bitmap object? Googling will give you tons of results how to get contents of a file into a byte array. – Sarwar Erfan Dec 20 '11 at 05:17

2 Answers2

0

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.

-1

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.

code-jaff
  • 9,230
  • 4
  • 35
  • 56