0

I have image's sd card path. Now what are the next steps to convert image into byte array because I want to upload the image to server?

Thanks in advance.

user3527400
  • 35
  • 1
  • 8
  • Do NOT convert it to bytearray first. Nor use Bitmap to load it. Just read in in a byte buffer and write to outputstream in a loop. – greenapps Feb 02 '15 at 11:08

1 Answers1

1
public static byte[] toByteArray (Bitmap raw) {

    byte[] byteArray = null;

    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream ();
        raw.compress (Bitmap.CompressFormat.JPEG, 100, stream);
        byteArray = stream.toByteArray ();
    }
    catch (Exception e) {
        e.printStackTrace ();
    }

    return byteArray;
}
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
  • If you place a file in a Bitmap and then compress it to jpg in a certain quality you will end up with a picture probably in a different format and less quality. So this is not an answer to the question. – greenapps Feb 02 '15 at 11:20
  • You can also use PNG compress format which does not loss quality. Hope you know that. – Murtaza Khursheed Hussain Feb 02 '15 at 11:25
  • Yes i know. But the OP did not say he wanted PNG's. And still it is not needed to place a file in a bitmap first. All kind of changes can happen. Much better to just upload the file as is. Byte for byte. You could place the file in a bytearray without using an intermediate bitmap. Then all bytes of the file would be in the bytearray. Only if you do that your post would be an answer to the question. – greenapps Feb 02 '15 at 11:37
  • neither he said to convert into specific format. He just asked to convert into byte[] for uploading, although there are many ways to do that, you can also convert to base64 string, you can create sockets etc. So, stick to the question he wants to convert into byte[] so I answered. – Murtaza Khursheed Hussain Feb 02 '15 at 11:40