我无法在设备上使用API <= 22(使用Android Studio和Unity)中启动torch。为什么?

huangapple go评论108阅读模式
英文:

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&#39;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&#39;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.&lt;init&gt;(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(&quot;com.unity3d.player.UnityPlayer&quot;);
    AndroidJavaObject activity = unityPlayer.GetStatic&lt;AndroidJavaObject&gt;(&quot;currentActivity&quot;);
    AndroidJavaObject context = activity.Call&lt;AndroidJavaObject&gt;(&quot;getApplicationContext&quot;);
    javaObject = new AndroidJavaObject(&quot;com.mugames.torchlibrary.AndroidTrochClass&quot;);
    javaObject.Call(&quot;Init&quot;, context);
    javaObject.Call(&quot;TorchMode&quot;, true);
}

Also Here is my Java code(Name of JAVA class is AndroidTrochClass) for API<23

        try {
            if(camera== null &amp;&amp; parameters==null) {
                camera = Camera.open();
                parameters = camera.getParameters();

                List&lt;String&gt; 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(&quot;Unity&quot;, &quot;No Flash Available&quot;);
                }
                camera.setParameters(parameters);
            }
            if (state) {
                camera.startPreview();
            } else {
                camera.stopPreview();
            }
        }
        catch (Exception e)
        {
            Log.e(TAG, &quot;TorchMode: &quot;,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

找到了!我需要在清单文件中添加相机权限 我无法在设备上使用API <= 22(使用Android Studio和Unity)中启动torch。为什么?

英文:

Found!! I need to add Camera permission in manifest file 我无法在设备上使用API <= 22(使用Android Studio和Unity)中启动torch。为什么?

huangapple
  • 本文由 发表于 2020年8月17日 19:34:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63450000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定