sceneform-android-sdk icon indicating copy to clipboard operation
sceneform-android-sdk copied to clipboard

Texture file loading

Open michaelvogt opened this issue 8 years ago • 10 comments

I try to load a texture from external storage, but can't get it to work. I used both FileProvider and regular file:/// style URI.

Texture builder looks like this:

Texture.builder() .setSource(getContext(), photoURI) .setUsage(Texture.Usage.DATA) .build();

Effectively, I create the file URI like this: Uri photoURI = Uri.fromFile(new File(getExternalStorageDirectory(), path)); file:///storage/emulated/0/Touristar/Hidaka/hidakabreads.jpg

Which leads to a file not found exception java.lang.IllegalStateException: java.io.FileNotFoundException: /storage/emulated/0/Touristar/Hidaka/hidakabreads.jpg at com.google.ar.sceneform.rendering.Texture$LoadTextureTask.loadData(Texture.java:311) at com.google.ar.sceneform.rendering.AssetLoader.lambda$runLoadTask$0(AssetLoader.java:81) at com.google.ar.sceneform.rendering.-$$Lambda$AssetLoader$Jdb8M1vmtZpNIxZABL9oLMUs3g0.run(lambda) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:761) Caused by: java.io.FileNotFoundException: /storage/emulated/0/Touristar/Hidaka/hidakabreads.jpg at android.content.res.AssetManager.openAsset(Native Method) at android.content.res.AssetManager.open(AssetManager.java:347) at android.content.res.AssetManager.open(AssetManager.java:321) at com.google.ar.sceneform.utilities.LoadHelper.lambda$fileUriToInputStreamCreator$0$LoadHelper(Unknown Source) at com.google.ar.sceneform.utilities.a.call(Unknown Source) at com.google.ar.sceneform.rendering.Texture$LoadTextureTask.loadData(Texture.java:308) at com.google.ar.sceneform.rendering.AssetLoader.lambda$runLoadTask$0(AssetLoader.java:81)  at com.google.ar.sceneform.rendering.-$$Lambda$AssetLoader$Jdb8M1vmtZpNIxZABL9oLMUs3g0.run(lambda)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)  at java.lang.Thread.run(Thread.java:761) 

Using a FilePorvider with the File create like above it looks like this Uri photoURI = FileProvider.getUriForFile(getContext(), "com.example.fileprovider", new File(getExternalStorageDirectory(), path));

The resulting exception says, the content scheme is not supported. java.lang.IllegalArgumentException: Unable to parse url: 'content://eu.michaelvogt.ar.author.fileprovider/hidaka_data/hidakabreads.jpg' at com.google.ar.sceneform.utilities.LoadHelper.remoteUriToInputStreamCreator(Unknown Source) at com.google.ar.sceneform.utilities.LoadHelper.fromUri(Unknown Source) at com.google.ar.sceneform.rendering.Texture$Builder.setSource(Texture.java:383) at eu.michaelvogt.ar.author.PreviewFragment.lambda$makeUserPanel$7(PreviewFragment.java:158) at eu.michaelvogt.ar.author.-$$Lambda$PreviewFragment$uoU9MkgipiyRm_Wzg9yaV0SVyf0.onClick(lambda) at android.view.View.performClick(View.java:5610) at android.view.View$PerformClick.run(View.java:22277) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:159) at android.app.ActivityThread.main(ActivityThread.java:6097) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: java.net.MalformedURLException: unknown protocol: content at java.net.URL.(URL.java:596) at java.net.URL.(URL.java:486) at java.net.URL.(URL.java:435) at com.google.ar.sceneform.utilities.LoadHelper.remoteUriToInputStreamCreator(Unknown Source)  at com.google.ar.sceneform.utilities.LoadHelper.fromUri(Unknown Source)  at com.google.ar.sceneform.rendering.Texture$Builder.setSource(Texture.java:383)  at eu.michaelvogt.ar.author.PreviewFragment.lambda$makeUserPanel$7(PreviewFragment.java:158)  at eu.michaelvogt.ar.author.-$$Lambda$PreviewFragment$uoU9MkgipiyRm_Wzg9yaV0SVyf0.onClick(lambda)  at android.view.View.performClick(View.java:5610)  at android.view.View$PerformClick.run(View.java:22277)  at android.os.Handler.handleCallback(Handler.java:751)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:159)  at android.app.ActivityThread.main(ActivityThread.java:6097)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

Should this work that way or am I doing something stupid?

michaelvogt avatar Jun 06 '18 10:06 michaelvogt

@michaelvogt - in 1.0 we interpret all file:// uris as being relative to the assets folder which is why we fail to load from external storage. Consequently, in order to load a Texture from a Uri, you'll need to either put in in the assets folder (i.e. bundled into your apk), or host it online where it's available via an http:// scheme.

tpsiaki avatar Jun 08 '18 16:06 tpsiaki

Hi @tpsiaki ,

do you have any idea when this could be fixed? It's quite essential functionality when you need to use plenty of models and do not want to have giant APK, or just simply need runtime model loading with sort of control and models preloading.

terezo avatar Jun 13 '18 07:06 terezo

Hi, Like @terezo said, loading a texture file from external storage seems essential to me. And likewise I'd like to know if it will be fixed in an upcoming release.

dyavil avatar Jun 27 '18 14:06 dyavil

Yes this is on our plate. There is a workaround for this so you'd don't have to wait for our fix. You can pass in an InputStream and load the texture from any source.

    Callable<InputStream> loadTextureFromFile =
        () -> {
          try {
            File filename = ....;
            return new FileInputStream(filename);
          } catch (IOException ex) {
            throw new CompletionException(ex);
          }
        };

    Texture.builder()
        .setSource(loadTextureFromFile)
        .build();

malik-at-work avatar Jun 28 '18 20:06 malik-at-work

Thanks ! As that might be of use for others, updating the documentation here may limit duplicates on this issue.

dyavil avatar Jul 02 '18 07:07 dyavil

Fixed in Sceneform 1.4, Closing.

malik-at-work avatar Aug 03 '18 16:08 malik-at-work

@malik-at-work got this issue in sceneform 1.4.0 minSdK = 17 supportLibrary = 27.1.1 phone samsung s7 galaxy OS = 8.0.0

Fatal Exception: java.lang.IllegalStateException: Texture must use ARGB8 format. at com.google.ar.sceneform.rendering.Texture$LoadTextureTask.loadData(Texture.java:299) at com.google.ar.sceneform.rendering.AssetLoader.lambda$runLoadTask$0$AssetLoader(AssetLoader.java:81) at com.google.ar.sceneform.rendering.AssetLoader$$Lambda$0.run(Unknown Source:8) 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)

basitsaleemmb avatar Aug 13 '18 05:08 basitsaleemmb

Can you check to make sure the bitmap you created is in the ARGB8 format?

dsternfeld7 avatar Aug 13 '18 16:08 dsternfeld7

@basitsaleemmb I've opened a new issue to discuss the new issue, check out https://github.com/google-ar/sceneform-android-sdk/issues/254

malik-at-work avatar Aug 13 '18 16:08 malik-at-work

@tpsiaki can you explain how can I load a texture from URL? I want to apply different textures and want to place textures on server.

chnouman avatar Oct 23 '19 04:10 chnouman