HTML to APK Builder

ANDROIDAPI REFERENCE

AndroidAPI: take photos, camera preview, compress and save images

Reference for capturePhoto (rear and front), startCameraPreview, compressImage and saveToGallery, plus HTML file inputs and getUserMedia in generated apps.

Updated:

There are four ways to use the camera in a generated app. Pick the simplest one that fits:

Need Use Pro CAMERA permission
Take one photo with the phone’s camera app capturePhoto(cb) no not required
Take a selfie capturePhoto(cb, 'front') no required
Let the user choose “Camera” or a gallery image <input type="file" accept="image/*"> no not required
Show a live camera inside your page getUserMedia (web) no required
Show a native full-screen live preview startCameraPreview() yes required
Method Event Pro
capturePhoto cameraResult no
startCameraPreview, stopCameraPreview cameraPreviewResult yes
compressImage imageCompressResult no
saveToGallery gallerySaveResult yes

capturePhoto

AndroidAPI.capturePhoto(callbackId)          // rear camera, via the camera app
AndroidAPI.capturePhoto(callbackId, 'front') // front camera, inside your app

The rear version opens the phone’s camera app. The user takes a photo, confirms it and returns to your page. This path works without the CAMERA permission. If you declared CAMERA in the project anyway, Android requires it to be granted, and the app asks for it.

The 'front' version captures with the front camera without leaving your app. It requires CAMERA to be declared and granted.

Result event: cameraResult {callbackId, success, base64, error}. base64 is a complete data URL (data:image/jpeg;base64,...), so you can put it straight into an <img>.

Errors: cancelled, permission_denied, no_camera_app, capture_file_missing, camera_permission_not_declared, front_capture_failed, decode_failed, launch_failed.

window.addEventListener('cameraResult', e => {
  const r = e.detail;
  if (r.callbackId !== 'profile') return;
  takeButton.disabled = false;
  if (!r.success) {
    if (r.error !== 'cancelled') showMessage('Camera error: ' + r.error);
    return;
  }
  document.querySelector('#photo').src = r.base64;
});
takeButton.onclick = () => {
  takeButton.disabled = true; // one capture at a time
  AndroidAPI.capturePhoto('profile');
};

Photos from modern phones are large. Pass them through compressImage before storing or uploading.

startCameraPreview and stopCameraPreview

AndroidAPI.startCameraPreview()
AndroidAPI.stopCameraPreview()

Pro. Shows a live rear-camera view as a native layer above your page. It is not an HTML element: you cannot position it with CSS, draw on it or read its pixels. CAMERA must already be granted, so call requestPermission first.

Event: cameraPreviewResult {success, active, error}. Errors include pro_required and permission_denied.

window.addEventListener('cameraPreviewResult', e => {
  previewButton.textContent = e.detail.active ? 'Close camera' : 'Open camera';
  if (!e.detail.success) showMessage(e.detail.error);
});

If you need the camera image inside your layout (for example to scan or apply filters), use getUserMedia below instead.

compressImage

AndroidAPI.compressImage(base64Data, maxWidth, quality, callbackId)

Resizes and re-encodes an image on the native side, which is much faster than doing it in JavaScript. base64Data can be raw base64 or a data URL. maxWidth is in pixels (16–10000; smaller images are not enlarged). quality is the JPEG quality from 1 to 100.

Result event: imageCompressResult {callbackId, success, base64, width, height, bytes, error}. base64 is a JPEG data URL. Transparency is lost because the output is JPEG. Errors: invalid_data, decode_failed, image_too_large, compress_failed.

window.addEventListener('cameraResult', e => {
  if (e.detail.success) AndroidAPI.compressImage(e.detail.base64, 1280, 80, 'small');
});
window.addEventListener('imageCompressResult', e => {
  const r = e.detail;
  if (r.callbackId !== 'small' || !r.success) return;
  preview.src = r.base64;
  info.textContent = `${r.width}×${r.height}, ${Math.round(r.bytes / 1024)} KB`;
});

saveToGallery

AndroidAPI.saveToGallery(fileName, base64Image, callbackId)

Pro. Saves an image to the phone’s gallery (Pictures folder) as PNG. base64Image can be raw base64 or a data URL, for example from canvas.toDataURL(). No storage permission is needed on current Android versions.

Result event: gallerySaveResult {callbackId, success, uri, error}. Show “Saved” only after success is true.

window.addEventListener('gallerySaveResult', e => {
  showMessage(e.detail.success ? 'Saved to gallery' : 'Could not save: ' + e.detail.error);
});
saveButton.onclick = () => {
  const dataUrl = document.querySelector('canvas').toDataURL('image/png');
  AndroidAPI.saveToGallery('drawing-' + Date.now() + '.png', dataUrl, 'save-1');
};

HTML file input with camera

<input type="file" accept="image/*">

A normal file input works in generated apps. When it accepts images, Android’s chooser offers Camera next to the gallery and files. The chosen photo arrives in input.files like in a browser. It needs no Pro and no CAMERA permission.

document.querySelector('input[type=file]').addEventListener('change', e => {
  const file = e.target.files[0];
  if (file) document.querySelector('#photo').src = URL.createObjectURL(file);
});

Live camera with getUserMedia

const stream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' } });
video.srcObject = stream;

Standard web camera access gives you live pixels inside your page, for QR scanning, filters or video calls. Declare CAMERA (and RECORD_AUDIO if you also request audio) in the project; without the declaration the browser call fails with NotAllowedError. The app shows the permission dialog when the page asks.

Stop all tracks with stream.getTracks().forEach(t => t.stop()) when the user leaves the camera screen. Do not run getUserMedia and startCameraPreview at the same time.