Konfigurowanie sesji ARCore na Androidzie
Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
Skonfiguruj sesję ARCore, aby tworzyć funkcje AR w swojej aplikacji.
Co to jest sesja?
Wszystkie procesy AR, takie jak śledzenie ruchu,
szacowanie wpływu na środowisko i szacowanie oświetlenia odbywa się wewnątrz ARCore.
. Session
to główny punkt wejścia do ARCore
API. Zarządza stanem systemu AR i obsługuje cykl życia sesji, dzięki czemu
w aplikacji, aby utworzyć, skonfigurować, rozpocząć lub zatrzymać sesję. Co najważniejsze,
umożliwia aplikacji odbieranie ramek, które umożliwiają dostęp do obrazu z aparatu
w pozycji urządzenia.
Tej sesji można używać do konfigurowania tych funkcji:
Sprawdź, czy aplikacja ARCore jest zainstalowana i aktualna
Zanim utworzysz Session
, sprawdź, czy aplikacja ARCore jest zainstalowana i aktualna. Jeśli
Aplikacja ARCore nie jest zainstalowana, tworzenie sesji kończy się niepowodzeniem i kolejna instalacja
lub uaktualnienie ARCore wymaga ponownego uruchomienia aplikacji.
Java
// Verify that ARCore is installed and using the current version.
private boolean isARCoreSupportedAndUpToDate() {
ArCoreApk.Availability availability = ArCoreApk.getInstance().checkAvailability(this);
switch (availability) {
case SUPPORTED_INSTALLED:
return true;
case SUPPORTED_APK_TOO_OLD:
case SUPPORTED_NOT_INSTALLED:
try {
// Request ARCore installation or update if needed.
ArCoreApk.InstallStatus installStatus = ArCoreApk.getInstance().requestInstall(this, true);
switch (installStatus) {
case INSTALL_REQUESTED:
Log.i(TAG, "ARCore installation requested.");
return false;
case INSTALLED:
return true;
}
} catch (UnavailableException e) {
Log.e(TAG, "ARCore not installed", e);
}
return false;
case UNSUPPORTED_DEVICE_NOT_CAPABLE:
// This device is not supported for AR.
return false;
case UNKNOWN_CHECKING:
// ARCore is checking the availability with a remote query.
// This function should be called again after waiting 200 ms to determine the query result.
case UNKNOWN_ERROR:
case UNKNOWN_TIMED_OUT:
// There was an error checking for AR availability. This may be due to the device being offline.
// Handle the error appropriately.
}
}
Kotlin
// Verify that ARCore is installed and using the current version.
fun isARCoreSupportedAndUpToDate(): Boolean {
return when (ArCoreApk.getInstance().checkAvailability(this)) {
Availability.SUPPORTED_INSTALLED -> true
Availability.SUPPORTED_APK_TOO_OLD, Availability.SUPPORTED_NOT_INSTALLED -> {
try {
// Request ARCore installation or update if needed.
when (ArCoreApk.getInstance().requestInstall(this, true)) {
InstallStatus.INSTALL_REQUESTED -> {
Log.i(TAG, "ARCore installation requested.")
false
}
InstallStatus.INSTALLED -> true
}
} catch (e: UnavailableException) {
Log.e(TAG, "ARCore not installed", e)
false
}
}
Availability.UNSUPPORTED_DEVICE_NOT_CAPABLE ->
// This device is not supported for AR.
false
Availability.UNKNOWN_CHECKING -> {
// ARCore is checking the availability with a remote query.
// This function should be called again after waiting 200 ms to determine the query result.
}
Availability.UNKNOWN_ERROR, Availability.UNKNOWN_TIMED_OUT -> {
// There was an error checking for AR availability. This may be due to the device being offline.
// Handle the error appropriately.
}
}
}
Tworzenie sesji
Utwórz i skonfiguruj sesję w ARCore.
Java
public void createSession() {
// Create a new ARCore session.
session = new Session(this);
// Create a session config.
Config config = new Config(session);
// Do feature-specific operations here, such as enabling depth or turning on
// support for Augmented Faces.
// Configure the session.
session.configure(config);
}
Kotlin
fun createSession() {
// Create a new ARCore session.
session = Session(this)
// Create a session config.
val config = Config(session)
// Do feature-specific operations here, such as enabling depth or turning on
// support for Augmented Faces.
// Configure the session.
session.configure(config)
}
Zamykanie sesji
Session
posiada znaczną ilość natywnej pamięci sterty. Brak wyraźnej informacji
zamknięcie sesji może spowodować wyczerpanie się pamięci natywnej i awarię aplikacji. Kiedy
sesja AR nie jest już potrzebna, wywołaj
close()
do opublikowania
i zasobami Google Cloud. Jeśli aplikacja zawiera jedną aktywność obsługującą AR, zadzwoń pod numer close()
z metody onDestroy()
aktywności.
Java
// Release native heap memory used by an ARCore session.
session.close();
Kotlin
// Release native heap memory used by an ARCore session.
session.close()
Dalsze kroki
O ile nie stwierdzono inaczej, treść tej strony jest objęta licencją Creative Commons – uznanie autorstwa 4.0, a fragmenty kodu są dostępne na licencji Apache 2.0. Szczegółowe informacje na ten temat zawierają zasady dotyczące witryny Google Developers. Java jest zastrzeżonym znakiem towarowym firmy Oracle i jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2025-07-26 UTC.
[null,null,["Ostatnia aktualizacja: 2025-07-26 UTC."],[[["\u003cp\u003eARCore sessions manage all AR processes like motion tracking and environmental understanding, and are accessed via the \u003ccode\u003eSession\u003c/code\u003e object.\u003c/p\u003e\n"],["\u003cp\u003eBefore creating an ARCore session, ensure ARCore is installed and up-to-date on the device to prevent session creation failures.\u003c/p\u003e\n"],["\u003cp\u003eTo configure features like lighting estimation or cloud anchors, utilize the \u003ccode\u003eConfig\u003c/code\u003e object and apply it to the session.\u003c/p\u003e\n"],["\u003cp\u003eTo prevent memory leaks and crashes, explicitly close the session using \u003ccode\u003esession.close()\u003c/code\u003e when it's no longer needed, such as in the activity's \u003ccode\u003eonDestroy()\u003c/code\u003e method.\u003c/p\u003e\n"]]],[],null,["# Configure an ARCore session in Android\n\nConfigure an ARCore session to build AR experiences for your app.\n\nWhat is a session?\n------------------\n\nAll [AR processes](/ar/discover/concepts), such as motion tracking,\nenvironmental understanding, and lighting estimation, happen inside an ARCore\nsession. [`Session`](/ar/reference/java/com/google/ar/core/Session) is the main entry point to the ARCore\nAPI. It manages the AR system state and handles the session lifecycle, allowing\nthe app to create, configure, start, or stop a session. Most importantly, it\nenables the app to receive frames that allow access to the camera image and\ndevice pose.\n\nThe session can be used to configure the following features:\n\n- [Lighting Estimation](/ar/develop/lighting-estimation)\n- [Cloud Anchors](/ar/develop/cloud-anchors)\n- [Augmented Images](/ar/develop/augmented-images)\n- [Augmented Faces](/ar/develop/augmented-faces)\n- [Depth API](/ar/develop/depth)\n- [Instant Placement](/ar/develop/instant-placement)\n- [ARCore Geospatial API](/ar/develop/geospatial)\n\nVerify that ARCore is installed and up to date\n----------------------------------------------\n\nBefore creating a `Session`, verify that ARCore is installed and up to date. If\nARCore isn't installed, session creation fails and any subsequent installation\nor upgrade of ARCore requires an app restart. \n\n### Java\n\n // Verify that ARCore is installed and using the current version.\n private boolean isARCoreSupportedAndUpToDate() {\n ArCoreApk.Availability availability = ArCoreApk.getInstance().checkAvailability(this);\n switch (availability) {\n case SUPPORTED_INSTALLED:\n return true;\n\n case SUPPORTED_APK_TOO_OLD:\n case SUPPORTED_NOT_INSTALLED:\n try {\n // Request ARCore installation or update if needed.\n ArCoreApk.InstallStatus installStatus = ArCoreApk.getInstance().requestInstall(this, true);\n switch (installStatus) {\n case INSTALL_REQUESTED:\n Log.i(TAG, \"ARCore installation requested.\");\n return false;\n case INSTALLED:\n return true;\n }\n } catch (UnavailableException e) {\n Log.e(TAG, \"ARCore not installed\", e);\n }\n return false;\n\n case UNSUPPORTED_DEVICE_NOT_CAPABLE:\n // This device is not supported for AR.\n return false;\n\n case UNKNOWN_CHECKING:\n // ARCore is checking the availability with a remote query.\n // This function should be called again after waiting 200 ms to determine the query result.\n case UNKNOWN_ERROR:\n case UNKNOWN_TIMED_OUT:\n // There was an error checking for AR availability. This may be due to the device being offline.\n // Handle the error appropriately.\n }\n }\n\n### Kotlin\n\n // Verify that ARCore is installed and using the current version.\n fun isARCoreSupportedAndUpToDate(): Boolean {\n return when (ArCoreApk.getInstance().checkAvailability(this)) {\n Availability.SUPPORTED_INSTALLED -\u003e true\n Availability.SUPPORTED_APK_TOO_OLD, Availability.SUPPORTED_NOT_INSTALLED -\u003e {\n try {\n // Request ARCore installation or update if needed.\n when (ArCoreApk.getInstance().requestInstall(this, true)) {\n InstallStatus.INSTALL_REQUESTED -\u003e {\n Log.i(TAG, \"ARCore installation requested.\")\n false\n }\n InstallStatus.INSTALLED -\u003e true\n }\n } catch (e: UnavailableException) {\n Log.e(TAG, \"ARCore not installed\", e)\n false\n }\n }\n\n Availability.UNSUPPORTED_DEVICE_NOT_CAPABLE -\u003e\n // This device is not supported for AR.\n false\n\n Availability.UNKNOWN_CHECKING -\u003e {\n // ARCore is checking the availability with a remote query.\n // This function should be called again after waiting 200 ms to determine the query result.\n }\n Availability.UNKNOWN_ERROR, Availability.UNKNOWN_TIMED_OUT -\u003e {\n // There was an error checking for AR availability. This may be due to the device being offline.\n // Handle the error appropriately.\n }\n }\n }\n\nCreate a session\n----------------\n\nCreate and configure a session in ARCore. \n\n### Java\n\n public void createSession() {\n // Create a new ARCore session.\n session = new Session(this);\n\n // Create a session config.\n Config config = new Config(session);\n\n // Do feature-specific operations here, such as enabling depth or turning on\n // support for Augmented Faces.\n\n // Configure the session.\n session.configure(config);\n }\n\n### Kotlin\n\n fun createSession() {\n // Create a new ARCore session.\n session = Session(this)\n\n // Create a session config.\n val config = Config(session)\n\n // Do feature-specific operations here, such as enabling depth or turning on\n // support for Augmented Faces.\n\n // Configure the session.\n session.configure(config)\n }\n\nClose a session\n---------------\n\n`Session` owns a significant amount of native heap memory. Failure to explicitly\nclose the session may cause your app to run out of native memory and crash. When\nthe AR session is no longer needed, call\n[`close()`](/ar/reference/java/com/google/ar/core/Session#close()) to release\nresources. If your app contains a single AR-enabled activity, call `close()`\nfrom the activity's `onDestroy()` method. \n\n### Java\n\n // Release native heap memory used by an ARCore session.\n session.close();\n\n### Kotlin\n\n // Release native heap memory used by an ARCore session.\n session.close()\n\nNext steps\n----------\n\n- [ARCore quickstart for Android](/ar/develop/java/quickstart)"]]