Skip to content

Commit 6819c15

Browse files
committed
Android selfie App review
1 parent 01327d0 commit 6819c15

File tree

1 file changed

+118
-0
lines changed
  • content/learning-paths/smartphones-and-mobile/build-android-selfie-app-using-mediapipe-multimodality

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
title: Handle camera permissions
3+
weight: 3
4+
5+
### FIXED, DO NOT MODIFY
6+
layout: learningpathall
7+
---
8+
9+
## Run the app on your device
10+
11+
1. Connect your Android device to your computer via a USB data cable. If this is your first time running and debugging Android apps, follow [this guide](https://developer.android.com/studio/run/device#setting-up) and double check this checklist:
12+
13+
1. You have enabled USB debugging on your Android device following [this doc](https://developer.android.com/studio/debug/dev-options#Enable-debugging).
14+
15+
2. You have confirmed by tapping "OK" on your Android device when an "Allow USB debugging" dialog pops up, and checked "Always allow from this computer".
16+
17+
![Allow USB debugging dialog](https://ftc-docs.firstinspires.org/en/latest/_images/AllowUSBDebugging.jpg)
18+
19+
20+
2. Make sure your device model name and SDK version correctly show up on the top right toolbar. Click the "Run" button to build and run the app.
21+
22+
3. After a while, you should see a success notification in Android Studio and the app showing up on your Android device.
23+
24+
4. However, the app shows only a black screen while printing error messages in your [Logcat](https://developer.android.com/tools/logcat) which looks like this:
25+
26+
```
27+
2024-11-20 11:15:00.398 18782-18818 Camera2CameraImpl com.example.holisticselfiedemo E Camera reopening attempted for 10000ms without success.
28+
2024-11-20 11:30:13.560 667-707 BufferQueueProducer pid-667 E [SurfaceView - com.example.holisticselfiedemo/com.example.holisticselfiedemo.MainActivity#0](id:29b00000283,api:4,p:2657,c:667) queueBuffer: BufferQueue has been abandoned
29+
2024-11-20 11:36:13.100 20487-20499 isticselfiedem com.example.holisticselfiedemo E Failed to read message from agent control socket! Retrying: Bad file descriptor
30+
2024-11-20 11:43:03.408 2709-3807 PackageManager pid-2709 E Permission android.permission.CAMERA isn't requested by package com.example.holisticselfiedemo
31+
```
32+
33+
5. Do not worry. This is expected behavior because you haven't correctly configured this app's [permissions](https://developer.android.com/guide/topics/permissions/overview) yet. Android OS restricts this app's access to camera features due to privacy reasons.
34+
35+
## Request camera permission at runtime
36+
37+
1. Navigate to `manifest.xml` in your `app` subproject's `src/main` path. Declare camera hardware and permission by inserting the following lines into the `<manifest>` element. Make sure it's declared outside and above `<application>` element.
38+
39+
```xml
40+
<uses-feature
41+
android:name="android.hardware.camera"
42+
android:required="true" />
43+
<uses-permission android:name="android.permission.CAMERA" />
44+
```
45+
46+
2. Navigate to `strings.xml` in your `app` subproject's `src/main/res/values` path. Insert the following lines of text resources, which will be used later.
47+
48+
```xml
49+
<string name="permission_request_camera_message">Camera permission is required to recognize face and hands</string>
50+
<string name="permission_request_camera_rationale">To grant Camera permission to this app, please go to system settings</string>
51+
```
52+
53+
3. Navigate to `MainActivity.kt` and add the following permission related values to companion object:
54+
55+
```kotlin
56+
// Permissions
57+
private val PERMISSIONS_REQUIRED = arrayOf(Manifest.permission.CAMERA)
58+
private const val REQUEST_CODE_CAMERA_PERMISSION = 233
59+
```
60+
61+
4. Add a new method named `hasPermissions()` to check on runtime whether camera permission has been granted:
62+
63+
```kotlin
64+
private fun hasPermissions(context: Context) = PERMISSIONS_REQUIRED.all {
65+
ContextCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
66+
}
67+
```
68+
69+
5. Add a condition check in `onCreate()` wrapping `setupCamera()` method, to request camera permission on runtime.
70+
71+
```kotlin
72+
if (!hasPermissions(baseContext)) {
73+
requestPermissions(
74+
arrayOf(Manifest.permission.CAMERA),
75+
REQUEST_CODE_CAMERA_PERMISSION
76+
)
77+
} else {
78+
setupCamera()
79+
}
80+
```
81+
82+
6. Override `onRequestPermissionsResult` method to handle permission request results:
83+
84+
```kotlin
85+
override fun onRequestPermissionsResult(
86+
requestCode: Int,
87+
permissions: Array<out String>,
88+
grantResults: IntArray
89+
) {
90+
when (requestCode) {
91+
REQUEST_CODE_CAMERA_PERMISSION -> {
92+
if (PackageManager.PERMISSION_GRANTED == grantResults.getOrNull(0)) {
93+
setupCamera()
94+
} else {
95+
val messageResId =
96+
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA))
97+
R.string.permission_request_camera_rationale
98+
else
99+
R.string.permission_request_camera_message
100+
Toast.makeText(baseContext, getString(messageResId), Toast.LENGTH_LONG).show()
101+
}
102+
}
103+
else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
104+
}
105+
}
106+
```
107+
108+
## Verify camera permission
109+
110+
1. Rebuild and run the app. Now you should see a dialog pop up requesting camera permissions!
111+
112+
2. Tap `Allow` or `While using the app` (depending on your Android OS versions). Then you should see your own face in the camera preview. Good job!
113+
114+
{{% notice Tip %}}
115+
Sometimes you might need to restart the app to observe the permission change take effect.
116+
{{% /notice %}}
117+
118+
In the next section, you will learn how to integrate MediaPipe vision solutions.

0 commit comments

Comments
 (0)