Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-328 android: handle photo from camera #857

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

LuckyLuky
Copy link

Platforms affected

Android

Motivation and Context

Allows users to use camera, not just file browser.

Closes #328.

Description

Added new ImageCapture intent and an option to choose between file browser and camera.

Testing

For image upload, I've tried to add image from file browser and camera. I've also tested changing from one to another.

Checklist

  • I've run the tests to see all new and existing tests pass
  • I added automated test coverage as appropriate for this change
  • Commit is prefixed with (platform) if this change only applies to one platform (e.g. (android))
  • If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct keyword to close issues using keywords)
  • I've updated the documentation if necessary

moklett added a commit to servus/cordova-plugin-inappbrowser that referenced this pull request Mar 18, 2022
This builds on LuckyLuky's [commit](LuckyLuky@876a12a) in [PR apache#857](apache#857) to improve the camera path for file inputs (`<input type="file"/>`) on Android.

The changes are:

- Light refactoring to extract work such as configuring the intent into functions
- Only offer the "Image Capture" intent if the device has a camera app and the file input has an accept type of an image (e.g. "image/png" or "image/*")
- Use a cache directory to store files, rather than the external storage. This mimics how cordova-plugin-camera stores files.
- Mark the photos for later deletion with `deleteOnExit`. I'm not sure how robust this is, so if we truly wanted to clean up files, we would need to do it explicitly, each time `onShowFileChooser` was fired.

Both the change in the original commit and in this one rely on the FileProvider API to allow the passing of content URIs (rather than File URIs) between this plugin and the camera app. As such, the FileProvider must be configured. The Camera Plugin does this in plugin.xml, but no such change has been made here yet. Therefore, configuration similar to the following must be added to the main app.

```xml
<!-- config.xml -->
<widget>
  ...
  <platform name="android">
    <!-- Configure our FileProvider-->
    <config-file target="AndroidManifest.xml" parent="application">
      <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="YOUR_APP_ID.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/camera_provider_paths"
        />
      </provider>
    </config-file>
    <resource-file src="camera_provider_paths.xml" target="app/src/main/res/xml/camera_provider_paths.xml" />
  </platform>
  ...
</widget>
```

```xml
<!-- camera_provider_paths.xml -->
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <cache-path name="cache_files" path="." />
</paths>
```

Fore more information on the above configuration, see the ["Take photos" Guide on Android Developers](https://developer.android.com/training/camera/photobasics)

The following improvements could be made to this feature of the plugin:

- Use the `captureEnabled()` boolean on the file chooser params to only offer the camera when capture is enabled on the HTML element
- Image downscaling - it would be very nice to allow control over the quality and size of the captured image. But, at this point, could we just delegate to the existing Camera plugin? (cordova-plugin-camera)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can not open camera when I click input type="file" on Android devices
2 participants