Pick a Photo
Lynx has no <input type="file">, so a web app's way of choosing a photo doesn't work in a Lynx app. LynxImagePicker opens the phone's own photo picker instead, and hands back the photo as a Blob, which the upload libraries you already use, such as Firebase Storage, can send as it is.
Let someone pick a photo
pick() resolves with { blob, name, type }, such as IMG_0042.jpg and image/jpeg, or with null when the person closes the picker without choosing.
To show the photo, give an <image> its object URL, as you'd give an <img> one on the web. The <image> element can't load a blob: URL itself, so AngularLynx hands it the photo's bytes as a data: URL instead. That copies the whole photo, so pick it scaled down, as above, when you only show it.
Upload it
The Blob goes straight into an upload. With Firebase Storage:
Or with HttpClient, which sends the Blob's type as the Content-Type:
Both send the bytes through AngularLynx's fetch and XMLHttpRequest, which pass them to the background thread to send (see HTTP Requests).
Choose the size and format
- With either option, the photo comes back as a JPEG. It's upright, whatever way the camera stored it, and it leaves out the original's metadata, such as where it was taken. A transparent image gets a white background, since a JPEG has no transparency.
- Without options, a JPEG, PNG, GIF or WebP comes back as it was picked. Anything else, such as the HEIC photos iPhones take, is re-encoded as a JPEG at full size, since browsers and Android phones can't all show it.
- Ask for the size you need. A photo straight from a 12-megapixel camera is several megabytes, and it crosses from native code to the background thread and then to your app as text. Scaled to 1024 pixels, it's a few hundred kilobytes and arrives at once.
What people see
- On iOS, the system photo picker (
PHPickerViewController). It runs outside your app and gives it only the photo picked, so your app needs no photo library permission and nophotosentry in angular.json. - On Android, the Photo Picker, which needs no permission either. Phones on Android 10 and older without it get it from Google Play services, or else the system's file picker.
- In the web preview, the browser's file dialog, from an
<input type="file" accept="image/*">. Browsers open it only in answer to a tap, so callpick()from a tap handler, not after waiting for something else.
When it doesn't return a photo
Closing the picker isn't an error: pick() resolves with null. It rejects with an ImagePickerError, whose code says why:
Lynx Explorer has no photo picker, so there pick() rejects with unavailable. Try it on a simulator, emulator or phone with ng serve --platform ios or --platform android, or in the web preview. To use it in your own native app, see Embed in a Native App.
Test it
In a spec there's no picker to tap through, so provide a stand-in that resolves with the photo the test needs:
Return null from the stand-in to test what happens when the person closes the picker. Without a stand-in, pick() rejects with unavailable, as in Lynx Explorer.
