英文:
I can't start torch in device with API <=22(Used Android Studio and Unity) .why?
问题
大家好,我正在尝试在Unity中创建一个使用Torch的Torch应用程序。根据文档,我注意到setTorchMode()
仅适用于API>22。对于API<23,我遵循了这里的方法。但是,即使在API<23中,我的应用程序也无法启动手电筒。通过Logcat,我追踪到了CameraService: Permission Denial: can't use the camera
的错误信息。
详细错误报告:
08-18 09:27:22.343 758-835/? I/ActivityManager: Killing
14121:com.google.android.partnersetup/u0a7 (adj 15): DHA:empty #19
08-18 09:27:22.343 196-2615/? W/ServiceManager: Permission failure:
android.permission.CAMERA from uid=10242 pid=14010
08-18 09:27:22.343 196-2615/? E/CameraService: Permission Denial: can't
use the camera pid=14010, uid=10242
08-18 09:27:22.343 14010-14051/? W/CameraBase: An error occurred while
connecting to camera: 0
08-18 09:27:22.353 14010-14051/? E/ContentValues: TorchMode:
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.<init>(Camera.java:568)
at android.hardware.Camera.open(Camera.java:426)
at
com.mugames.torchlibrary.AndroidTrochClass.TorchMode
(AndroidTrochClass.java:64)
at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
at com.unity3d.player.UnityPlayer.access$300(Unknown Source)
at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:145)
at com.unity3d.player.UnityPlayer$e.run(Unknown Source)
这是我的C#(Unity代码):
private void Awake()
{
if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
{
Permission.RequestUserPermission(Permission.Camera);
}
}
void Start()
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject context = activity.Call<AndroidJavaObject>("getApplicationContext");
javaObject = new AndroidJavaObject("com.mugames.torchlibrary.AndroidTrochClass");
javaObject.Call("Init", context);
javaObject.Call("TorchMode", true);
}
这是我用于API<23的Java代码(Java类名为AndroidTrochClass):
try {
if (camera == null && parameters == null) {
camera = Camera.open();
parameters = camera.getParameters();
List<String> modesAvailable = parameters.getSupportedFlashModes();
if (modesAvailable.contains(Camera.Parameters.FLASH_MODE_TORCH)) {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
} else if (modesAvailable.contains((Camera.Parameters.FLASH_MODE_ON))) {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
} else {
Log.d("Unity", "No Flash Available");
}
camera.setParameters(parameters);
}
if (state) {
camera.startPreview();
} else {
camera.stopPreview();
}
} catch (Exception e) {
Log.e(TAG, "TorchMode: ", e);
e.printStackTrace();
}
注意:我将Java类仅用作插件,因此Java类未继承任何内容,所以我已将当前上下文传递给了Java方法。
英文:
Hi friends I'm trying to make torch application in unity.From documentation I have noticed that setTorchMode() is applicable only if we have API>22. It works fine for API>22.But for below API<23 I have followed this. But even though my application in not staring torch in API<23 . From Logcat I have traced that CameraService: Permission Denial: can't use the camera
Detail Error Report:
08-18 09:27:22.343 758-835/? I/ActivityManager: Killing
14121:com.google.android.partnersetup/u0a7 (adj 15): DHA:empty #19
08-18 09:27:22.343 196-2615/? W/ServiceManager: Permission failure:
android.permission.CAMERA from uid=10242 pid=14010
08-18 09:27:22.343 196-2615/? E/CameraService: Permission Denial: can't
use the camera pid=14010, uid=10242
08-18 09:27:22.343 14010-14051/? W/CameraBase: An error occurred while
connecting to camera: 0
08-18 09:27:22.353 14010-14051/? E/ContentValues: TorchMode:
java.lang.RuntimeException: Fail to connect to camera service
`enter code here`at android.hardware.Camera.<init>(Camera.java:568)
at android.hardware.Camera.open(Camera.java:426)
at
com.mugames.torchlibrary.AndroidTrochClass.TorchMode
(AndroidTrochClass.java:64)
at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
at com.unity3d.player.UnityPlayer.access$300(Unknown Source)
at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:145)
at com.unity3d.player.UnityPlayer$e.run(Unknown Source)
Here is my C# (Unity Code)
private void Awake()
{
if(!Permission.HasUserAuthorizedPermission(Permission.Camera))
{
Permission.RequestUserPermission(Permission.Camera);
}
}
void Start()
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject context = activity.Call<AndroidJavaObject>("getApplicationContext");
javaObject = new AndroidJavaObject("com.mugames.torchlibrary.AndroidTrochClass");
javaObject.Call("Init", context);
javaObject.Call("TorchMode", true);
}
Also Here is my Java code(Name of JAVA class is AndroidTrochClass) for API<23
try {
if(camera== null && parameters==null) {
camera = Camera.open();
parameters = camera.getParameters();
List<String> modesAvailable = parameters.getSupportedFlashModes();
if (modesAvailable.contains(Camera.Parameters.FLASH_MODE_TORCH)) {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
} else if (modesAvailable.contains((Camera.Parameters.FLASH_MODE_ON))) {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
} else {
Log.d("Unity", "No Flash Available");
}
camera.setParameters(parameters);
}
if (state) {
camera.startPreview();
} else {
camera.stopPreview();
}
}
catch (Exception e)
{
Log.e(TAG, "TorchMode: ",e);
e.printStackTrace();
}
NOTE: I'm using Java Class Just as Plugin So Java Class extend to nothing so I have passed current context to Java Methods
答案1
得分: 0
找到了!我需要在清单文件中添加相机权限
英文:
Found!! I need to add Camera permission in manifest file
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论