I take a picture using the android.hardware.Camera API. I then convert it to a Bitmap of half the actual size, compress it to a JPEG of quality 80, convert it to Base64 and send it to the server as follows.
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream); byte[] byteArray = byteArrayOutputStream.toByteArray(); String encoded = Base64.encodeToString(byteArray, Base64.NO_WRAP); String json_response = ""; try { URL url = new URL("https://example.com/api_endpoint"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(15000); conn.setConnectTimeout(15000); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); OutputStream os = conn.getOutputStream(); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8")); writer.write("?reg=" + regCode); writer.write("&img=" + encoded); writer.flush(); writer.close(); os.close(); Log.d("Auth", conn.getResponseCode() + ""); InputStreamReader in = new InputStreamReader(conn.getInputStream()); BufferedReader br = new BufferedReader(in); String text = ""; while ((text = br.readLine()) != null) { json_response += text; } conn.disconnect(); } catch (IOException e) { Log.d(getClass().getName(), "" + e.getMessage()); } This works as expected. Now, If I don't resize the image and keep the quality 100%, how should I go about to avoid an OutOfMemoryError? My application requires the image to be in the full resolution and best quality possible.
My questions are:
- Is the way I am uploading the correct way?
- How to send Image is best quality without
OutOfMemoryErrori.e. how to optimize RAM usage in this process?