LibGDX - 从本地存储加载 AssetManager 中的文件
LibGDX - load file in AssetManager from local storage
我有一个模型在本地存储 (/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.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.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.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
。
这不是一个完美的解决方案,但它确实有效。 AssetManager
有一个接受 FileHandleResolver
作为参数的构造函数。
AssetManager assets = new AssetManager(new LocalFileHandleResolver());
这不是一个理想的解决方案,因为如果您要使用本地存储和内部存储的混合资产,则需要为每个创建 AssetManager
。
我有一个模型在本地存储 (/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.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.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.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
。
这不是一个完美的解决方案,但它确实有效。 AssetManager
有一个接受 FileHandleResolver
作为参数的构造函数。
AssetManager assets = new AssetManager(new LocalFileHandleResolver());
这不是一个理想的解决方案,因为如果您要使用本地存储和内部存储的混合资产,则需要为每个创建 AssetManager
。