Search code examples
androidandroid-imageandroid-bitmapandroid-service-binding

Converting Image to bytearray in order to upload image to server Android


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.


Solution

  • 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;
    }