Android Camera: require frontal OR rear camera

2019-04-26 12:41发布

问题:

My app needs a Camera to work. However, it doesn't matter if it's a rear or frontal camera.

Right now, I have this in my Manifest:

<uses-feature android:name="android.hardware.camera"/>

To require a frontal Camera, I know I could also add this:

<uses-feature android:name="android.hardware.camera.front"/>

But I'd like to support all devices that has EITHER camera. Is there anyway to do this?

For example, I want to support Nexus 7 which has only a frontal camera. But I also want to support devices with rear camera only.

According to some research I've made, such as:

http://code.google.com/p/android/issues/detail?id=35166

It seems this is not possible.

I think one way to solve this would be to make 2 separate APKs, one with android.hardware.camera and one with android.hardware.camera.front and upload both to Google Play, using its Multiple APK Support. I haven't tested it yet, though.

Has anybody found a recommended way to support all devices with a frontal camera, a rear camera or both, but not devices without any cameras?

回答1:

Several thoughts (no definite answer):

a) I believe you are right that currently there is no good way to require either camera in on APK.

b) If you want, you can remove uses-feature and check for a camera in runtime. It will make user experience worse, but it will work.

c) You can use use required="false" (http://developer.android.com/guide/topics/manifest/uses-feature-element.html). Hopefully, Google Market prioritize applications using this flag.

c) Side note. As I know most of Android devices have a camera. So, if you go with solution b) or c) only very small user base will notice the difference



回答2:

This has changed since it was answered, you can now add the uses-feature to the manifest that requires any camera like this:

<uses-feature android:name="android.hardware.camera.any" android:required="true" />


回答3:

Just to add my 2 cents to this discussion:

I achieved the same result as @Cat described in comment to Juan Cortes.

The way I tested this was using aapt (instructions on testing found in http://developer.android.com/guide/topics/manifest/uses-feature-element.html#permissions-features ) , and looking for explicit or implicit camera requirements:

No <uses-feature> flag, but requiring camera permission: Observe the uses-implied-feature

Using only <uses-feature android:name="android.hardware.camera.any" android:required="false" />: Still implying that the app ALWAYS needs a camera.

both <uses-feature android:name="android.hardware.camera.any" android:required="false" /><uses-feature android:name="android.hardware.camera" android:required="false" /> defined: No more implied features.

Hope this helps somebody profile their own app.