Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MalformedURLException: unknown protocol: android

I was trying to adapt the code from this: 3D-model-viewer to my app because I want to insert some models from that kind to show human anatomy.

Everything seems fine, I first cloned and tried the original repository, it worked, so I started using that code.

After adapting and checking everything's okay and there are no errors marked, I ran the app, but there seems to be an issue. In the original code, the person saves a url like this:

url = new URL("android://org.andresoviedo.dddmodel2/assets/" + parent.getParamAssetDir() + File.separator + parent.getParamAssetFilename());

I already changed the first part to my project:

url = new URL("android://com.example.ovman.appbotox/assets/" + parent.getParamAssetDir() + File.separator + parent.getParamAssetFilename());

so I don't think is that, algo I inserted some logs to verify the params aren't null.

The class in github is here: SceneLoader.java

So, when I get to that point, the Logcat says "unkown protocol: andorid". Here's the complete logcat:

06-26 17:34:26.304 26616-26616/com.example.ovman.appbotox E/SceneLoader: unknown protocol: android
java.net.MalformedURLException: unknown protocol: android
    at java.net.URL.<init>(URL.java:608)
    at java.net.URL.<init>(URL.java:498)
    at java.net.URL.<init>(URL.java:447)
    at com.example.ovman.appbotox.model3D.services.SceneLoader.init(SceneLoader.java:118)
    at com.example.ovman.appbotox.ZonesActivity.onCreate(ZonesActivity.java:108)
    at android.app.Activity.performCreate(Activity.java:7131)
    at android.app.Activity.performCreate(Activity.java:7122)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2882)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3037)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6642)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
06-26 17:34:26.533 26616-26616/com.example.ovman.appbotox E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ovman.appbotox, PID: 26616
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ovman.appbotox/com.example.ovman.appbotox.ZonesActivity}: java.lang.RuntimeException: java.net.MalformedURLException: unknown protocol: android
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2902)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3037)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6642)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.RuntimeException: java.net.MalformedURLException: unknown protocol: android
        at com.example.ovman.appbotox.model3D.services.SceneLoader.init(SceneLoader.java:124)
        at com.example.ovman.appbotox.ZonesActivity.onCreate(ZonesActivity.java:108)
        at android.app.Activity.performCreate(Activity.java:7131)
        at android.app.Activity.performCreate(Activity.java:7122)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2882)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3037) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6642) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
     Caused by: java.net.MalformedURLException: unknown protocol: android
        at java.net.URL.<init>(URL.java:608)
        at java.net.URL.<init>(URL.java:498)
        at java.net.URL.<init>(URL.java:447)
        at com.example.ovman.appbotox.model3D.services.SceneLoader.init(SceneLoader.java:118)
        at com.example.ovman.appbotox.ZonesActivity.onCreate(ZonesActivity.java:108) 
        at android.app.Activity.performCreate(Activity.java:7131) 
        at android.app.Activity.performCreate(Activity.java:7122) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2882) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3037) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6642) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

 

My repository is this, the app runs, there are no errors marked, just when selecting the 3rd option in my menu (it's the first screen) it crashes.

I kept the url line like that since I didn't know if the "android://" part was really important or not.

What do you think I should change or do you know what that error means? Do I need to import anything? I checked the original project (manifest, folder structure, gradle) and everything is similar, so I don't know where the mistake lies.

At the end, what I want is to access my assets folder to show some .obj files. And for the URL, I know that for links you should put a protocol, but I don't get why the protocol android isn't working for getting something in one of my project paths.

EDIT: In my repository I made some changes to get the obj file, still, if someone could tell me (as the modification hadn't anything to do with the url) how to manage that about the android protocol. I checked this link: URL but I couldn't find much.

like image 815
SistemasIntegrales Avatar asked Dec 23 '22 06:12

SistemasIntegrales


1 Answers

The "android" protocol is handled by a custom handler.

// Custom handler: org/andresoviedo/app/util/url/android/Handler.class
System.setProperty("java.protocol.handler.pkgs", "org.andresoviedo.app.util.url");

It is possible that for some reason, the System property that registers the custom handler is not working. So to force java to handle custom protocol, you can do it like this:

URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
    @Override
    public URLStreamHandler createURLStreamHandler(String protocol) {
       if ("android".equals(protocol)){
           return new Handler();
       }
       return null;
    }
});
like image 88
Andrés Oviedo Avatar answered Jan 11 '23 21:01

Andrés Oviedo