Android 13下如何在Android Studio中用Google Scene Viewer加载本地glTF文件?
Google Scene Viewer加载本地glTF文件的解决方案
Google Scene Viewer可以加载本地glTF文件,但不能直接传入绝对文件路径,必须通过Content URI提供安全的文件访问权限,这就是你直接用绝对路径导致加载失败的原因。
具体解决步骤:
生成Content URI
Android推荐使用FileProvider来生成本地文件的Content URI,确保跨应用访问的安全性:- 先在
AndroidManifest.xml中注册FileProvider:<provider android:name="androidx.core.content.FileProvider" android:authorities="com.your.package.name.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> - 在
res/xml目录下创建file_paths.xml,配置允许访问的文件目录(示例为外部存储根目录):<paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="external_files" path="." /> </paths> - 代码中生成目标glTF文件的Content URI:
File localGltfFile = new File("/storage/emulated/0/your_folder/scene.gltf"); Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.your.package.name.fileprovider", localGltfFile);
- 先在
修改Scene Viewer Intent
将生成的Content URI传入Intent,并添加临时读取权限:Intent sceneViewerIntent = new Intent(Intent.ACTION_VIEW); Uri intentUri = Uri.parse("https://arvr.google.com/scene-viewer/1.0").buildUpon() .appendQueryParameter("file", contentUri.toString()) .appendQueryParameter("mode", "ar_only") .build(); sceneViewerIntent.setData(intentUri); sceneViewerIntent.setPackage("com.google.ar.core"); // 授予Scene Viewer临时读取文件的权限 sceneViewerIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(sceneViewerIntent);额外检查项
- 确保glTF文件依赖的.bin、纹理等资源和glTF文件在同一目录下,且FileProvider的路径配置覆盖了该目录。
- 验证glTF文件本身是否合法,排除文件损坏、格式错误的可能。
内容的提问来源于stack exchange,提问作者Dimitris Giannakopoulos
相关产品推荐
相关产品推荐

