Office Hours Transcript: 2021-08-14

mabsten joined

hello, mabsten!

 

how can I help you today?

Hello. Sorry, I accidentally accessed the chat by browsing the commonsware.com site. Thanks anyway ! :)

mabsten left

 

eric joined

what’s up?

hello, eric!

 

how can I help you today?

In https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader.Builder addPathHandler what is argument "path" relative to?

I believe that refers to the path portion of a URL

 

so AFAIK it is not relative to anything, other than

 

(er, ignore that "other than" part)

Can you give an example? For example on https://stackoverflow.com/questions/40069737/how-to-use-android-internal-storage it shows "/data/user/0/com.ed.ilan.ioioterbium/files/documents/2016_9_16_10_28_12.csv", so what would be the value of path? The documentation is not clear

the value of path is whatever you want it to be – I suppose you could define it as being relative to https://appassets.androidplatform.net

 

and that Stack Overflow question does not seem to be related to WebViewAssetLoader, though perhaps I am misunderstanding your situation

 

so, in https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader, we have:

final WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
          .addPathHandler("/assets/", new AssetsPathHandler(this))
          .build();
 

that /assets/ is referring to the path segment in:

webview.loadUrl("https://appassets.androidplatform.net/assets/www/index.html");

in that example it is actually relative to main, so path is "assets". It wouldn’t work if asssets did not reside in main

well, all assets get merged together from the relevant build variants when the app is compiled

 

so, a debug build would merge main/assets/ and debug/assets/ into the assets packaged in the app

 

the comment above the loadUrl() line in that sample is using main/assets as a candidate place for assets to load from

 

but that example certainly could be made clearer

is the loadUrl line necessary?

you are going to need to get content into the WebView somehow – whether that is via loadUrl() or loadData() or loadDataWithBaseURL() is up to you

 

that particular loadUrl() call is trying to illustrate directly the type of URL that inside some Web page would point to an asset, instead of to actual Web content loaded from the Internet

 

and, if your actual HTML were to be loaded from assets, that is the sort of loadUrl() call you would make to load that asset into the WebView

From the docs it seems the purpose is to link to an asset stored locally using https://appassets.androidplatform.net instead of file://, isn’t loadUrl loading a webpage? If I just want to upload to appassets I dont see a reason to call loadUrl in addition to intercepting a request

isn’t loadUrl loading a webpage?

yes – in this case, the Web page itself is an asset

 

going back to your original question, if the opening Java were:

final WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
          .addPathHandler("/something-else/", new AssetsPathHandler(this))
          .build();

then the URL syntax you would use is: https://appassets.androidplatform.net/something-else/the/path/to/your/asset

is it assuming "something-else" is relative to a local directory? In ""/data/user/0/com.ed.ilan.ioioterbium/files/documents/2016_9_16_10_28_12.csv" can I use "/user" or "files". Do you see my point?

WebViewAssetLoader does not load from the filesystem

 

at least, AFAIK

 

if it does, I do not know how to set that up

 

oh wait

 

so the sample there is:

 File publicDir = new File(context.getFilesDir(), "public");
 // Host "files/public/" in app's data directory under:
 // http://appassets.androidplatform.net/public/...
 WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
          .addPathHandler("/public/", new InternalStoragePathHandler(context, publicDir))
          .build();
 

in this case, /public/ is still part of the URL, and you control where that maps to on the filesystem via the File that you pass to the InternalStoragePathHandler() constructor

 

you can have several handlers for a given WebViewAssetLoader, with different URL prefixes mapping to different things (e.g., one mapping to assets, one mapping to some filesystem directory)

 

it’s been several months since I played with WebViewAssetLoader, and there I only used it literally for assets – sorry that I am rusty on this

np I appreciate it

we’re about 10 minutes over the chat window, and I need to run an errand, so… that’s a wrap for today’s chat

 

the next one is Tuesday in the 7:30pm US Eastern time slot

 

have a pleasant day!