简体   繁体   中英

How to launch Camera app from Unity in android?

I want to launch camera app in android from Unity3d app. I can do it by building an Android app through Android Studio. I want to create a jar plugin of that android studio project and call the code through Unity game.

Unity C# code

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.NoClassDefFoundError:Failed resolution of: Landroidx/appcompat/app/AppCompatActivity

Here is my Android Studio code that worked for Android

    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. 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. I am using Unity version 2019.1.7f1.

Any help would be much appreciated.

Changing "AppCompatActivity" to just "Activity" worked for me, though I'm not sure why. If you ever figure out why, let me know!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM