简体   繁体   English

如何在 android 中从 Unity 启动相机应用程序?

[英]How to launch Camera app from Unity in android?

I want to launch camera app in android from Unity3d app.我想从 Unity3d 应用程序在 android 中启动相机应用程序。 I can do it by building an Android app through Android Studio.我可以通过 Android Studio 构建一个 Android 应用程序来实现。 I want to create a jar plugin of that android studio project and call the code through Unity game.我想创建一个 android studio 项目的 jar 插件并通过 Unity 游戏调用代码。

Unity C# code统一 C# 代码

public class UnityAndroidPluginExample : MonoBehaviour
{
    AndroidJavaObject javaClass;

    public void CallPlugin()
    {
        javaClass = new AndroidJavaObject("com.mycompany.pluginexample.CameraPluginClass");
        javaClass.Call("LaunchCameraApp");
    }
}

Android Studio Code安卓工作室代码

package com.mycompany.pluginexample;

import android.content.Intent;
import android.provider.MediaStore;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;

public class CameraPluginClass extends AppCompatActivity
{
    public void LaunchCameraApp()
    {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivity(intent);
    }
}

I am getting error我收到错误

AndroidJavaException: java.lang.ClassNotFoundException:com.mycompany.pluginexample.CameraPluginClass AndroidJavaException: java.lang.ClassNotFoundException:com.mycompany.pluginexample.CameraPluginClass

AndroidJavaException: java.lang.NoClassDefFoundError:Failed resolution of: Landroidx/appcompat/app/AppCompatActivity AndroidJavaException:java.lang.NoClassDefFoundError:解析失败:Landroidx/appcompat/app/AppCompatActivity

Here is my Android Studio code that worked for Android这是我适用于 Android 的 Android Studio 代码

    btnCaptureImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 0);
        }
    });

Please help, I am stuck here.请帮忙,我被困在这里。 In short, I cannot call a method of the Java class that extends AppCompatACtivity.简而言之,我无法调用扩展 AppCompatACtivity 的 Java 类的方法。 If I remove extends AppCompatActivity and call a method in that Java Class from Unity, it works, only if I use extends AppCompatActivty , I cannot call any method inside that class from Unity.如果我删除extends AppCompatActivity并从 Unity 调用该 Java 类中的方法,它会起作用,只有当我使用extends AppCompatActivty ,我才能从 Unity 调用该类中的任何方法。 I am using Unity version 2019.1.7f1.我正在使用 Unity 版本 2019.1.7f1。

Any help would be much appreciated.任何帮助将非常感激。

Changing "AppCompatActivity" to just "Activity" worked for me, though I'm not sure why.将“AppCompatActivity”更改为“Activity”对我有用,但我不确定为什么。 If you ever figure out why, let me know!如果你知道为什么,请告诉我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式从我的 Windows 10 应用程序启动相机应用程序? - How to launch camera app from my windows 10 app programmatically? 如何在 Unity Android 应用程序中使用 OpenCV 中的 ARCore 相机图像? - How to use the ARCore camera image in OpenCV in an Unity Android app? 我需要通过按下我的统一应用程序中的按钮来加载 android 相机应用程序 - I need to load android camera app by press on button from my unity application 有没有办法从Windows Phone 8.1应用程序启动手机摄像头应用程序 - Is there a way to launch phone camera app from Windows Phone 8.1 app 统一打开相机应用程序时如何访问设备的前置相机 - how to access the front camera of a device when camera app opens in unity 在 Unity 应用程序启动时读取 Android 意图额外数据 - Read Android intent extra data on Unity app launch 如何从 Unity Android 应用程序打开 Android 系统设置 - How to Open Android System Settings from Unity Android App 如何在Android上从电源按钮自动启动应用页面 - How to launch app page from power button automatically on Android 从我的应用程序启动Android视频应用程序 - Launch the android Video App from my app 如何从 Unity 设备中的 select 前置摄像头 - how to select front camera from device in Unity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM