LibGDX-从本地存储在AssetManager中加载文件

时间:2018-12-28 04:08:43

标签: android libgdx android-assets

我有一个本地存储(/data/user/0/com.myapp/files/model.g3db)中的模型。我想将此模型加载到我的AssetManager中,以便以后使用。 如果使用assetManager.load("model.obj", Model.class);之类的标准方法,它将从assets文件夹中获取一个文件,该文件是项目的一部分。这不适合我。因此,我尝试使用AssetDescriptor来指定文件:

FileHandle fh = Gdx.files.local("model.g3db");
if(fh.exists()) Gdx.app.log("file", "exists");
else Gdx.app.log("file", "not exists");

AssetDescriptor<Model> ad = new AssetDescriptor<Model>(fh, Model.class);
assets.load(ad);

输出:

I/file: exists

Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load dependencies of asset: model.g3db
                  at com.badlogic.gdx.assets.AssetLoadingTask.handleAsyncLoader(AssetLoadingTask.java:121)
                  at com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:90)
                  at com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:507)
                  at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:381)
                  at com.myapp.EngineCore.render(EngineCore.java:226) 
                  at com.badlogic.gdx.backends.android.AndroidGraphicsLiveWallpaper.onDrawFrame(AndroidGraphicsLiveWallpaper.java:220) 
                  at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1571) 
                  at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1270) 
               Caused by: com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.SerializationException: Error parsing file: model.g3db
                  at com.badlogic.gdx.utils.async.AsyncResult.get(AsyncResult.java:46)
                  at com.badlogic.gdx.assets.AssetLoadingTask.handleAsyncLoader(AssetLoadingTask.java:119)
                  at com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:90) 
                  at com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:507) 
                  at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:381) 
                  at com.nolesh.android.livewallpapers.iamrich.EngineCore.render(EngineCore.java:226) 
                  at com.badlogic.gdx.backends.android.AndroidGraphicsLiveWallpaper.onDrawFrame(AndroidGraphicsLiveWallpaper.java:220) 
                  at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1571) 
                  at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1270) 
               Caused by: com.badlogic.gdx.utils.SerializationException: Error parsing file: model.g3db
                  at com.badlogic.gdx.utils.UBJsonReader.parse(UBJsonReader.java:56)
                  at com.badlogic.gdx.graphics.g3d.loader.G3dModelLoader.parseModel(G3dModelLoader.java:65)
                  at com.badlogic.gdx.graphics.g3d.loader.G3dModelLoader.loadModelData(G3dModelLoader.java:61)
                  at com.badlogic.gdx.assets.loaders.ModelLoader.getDependencies(ModelLoader.java:75)
                  at com.badlogic.gdx.assets.loaders.ModelLoader.getDependencies(ModelLoader.java:35)
                  at com.badlogic.gdx.assets.AssetLoadingTask.call(AssetLoadingTask.java:64)
                  at com.badlogic.gdx.assets.AssetLoadingTask.call(AssetLoadingTask.java:34)
                  at com.badlogic.gdx.utils.async.AsyncExecutor$2.call(AsyncExecutor.java:58)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                  at java.lang.Thread.run(Thread.java:764)
               Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: model.g3db (Internal)
                  at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:77)
                  at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:151)
                  at com.badlogic.gdx.utils.UBJsonReader.parse(UBJsonReader.java:54)
                  at com.badlogic.gdx.graphics.g3d.loader.G3dModelLoader.parseModel(G3dModelLoader.java:65) 
                  at com.badlogic.gdx.graphics.g3d.loader.G3dModelLoader.loadModelData(G3dModelLoader.java:61) 
                  at com.badlogic.gdx.assets.loaders.ModelLoader.getDependencies(ModelLoader.java:75) 
                  at com.badlogic.gdx.assets.loaders.ModelLoader.getDependencies(ModelLoader.java:35) 
                  at com.badlogic.gdx.assets.AssetLoadingTask.call(AssetLoadingTask.java:64) 
                  at com.badlogic.gdx.assets.AssetLoadingTask.call(AssetLoadingTask.java:34) 
                  at com.badlogic.gdx.utils.async.AsyncExecutor$2.call(AsyncExecutor.java:58) 
                  at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
                  at java.lang.Thread.run(Thread.java:764) 
               Caused by: java.io.FileNotFoundException: model.g3db
                  at android.content.res.AssetManager.openAsset(Native Method)
                  at android.content.res.AssetManager.open(AssetManager.java:374)
                  at android.content.res.AssetManager.open(AssetManager.java:348)
                  at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:75)
                  at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:151) 
                  at com.badlogic.gdx.utils.UBJsonReader.parse(UBJsonReader.java:54) 
                  at com.badlogic.gdx.graphics.g3d.loader.G3dModelLoader.parseModel(G3dModelLoader.java:65) 
                  at com.badlogic.gdx.graphics.g3d.loader.G3dModelLoader.loadModelData(G3dModelLoader.java:61) 
                  at com.badlogic.gdx.assets.loaders.ModelLoader.getDependencies(ModelLoader.java:75) 
                  at com.badlogic.gdx.assets.loaders.ModelLoader.getDependencies(ModelLoader.java:35) 
                  at com.badlogic.gdx.assets.AssetLoadingTask.call(AssetLoadingTask.java:64) 
                  at com.badlogic.gdx.assets.AssetLoadingTask.call(AssetLoadingTask.java:34) 
                  at com.badlogic.gdx.utils.async.AsyncExecutor$2.call(AsyncExecutor.java:58) 
                  at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
                  at java.lang.Thread.run(Thread.java:764) 

当我将此模型移到项目的assets文件夹中时,它会成功加载。

因此,我的问题是如何从本地存储将模型加载到AssetManager

1 个答案:

答案 0 :(得分:0)

这不是一个完美的解决方案,但可以。 public void pressStop() { // boolean isStopped = false; if (isStopped) { currentSong = 0; System.out.println("Songs are stopped"); } else { System.out.println("Song " + currentSong + " is stopped"); } isStopped = true; } public void pressPlay() { // set the currentSong and do whatever isStopped = false; } 的构造函数接受AssetManager作为参数。

FileHandleResolver

这不是理想的解决方案,因为如果要使用本地存储和内部存储中的混合资产,则需要为每个存储创建AssetManager assets = new AssetManager(new LocalFileHandleResolver());