zontrakulla zontrakulla - 2 months ago 56
Android Question

Unable to open asset URL: file:///android_asset/activity_a

I just began android coding and I'm still learning from my mistakes. I use

WebView
to load internal html pages and I want to open another activity window which will be a
barcode scanner
by clicking a hyperlink on
webview
. However I get this error

Unable to open asset URL: file:///android_asset/activity_a://qrcodeactivity


AndroidManifest.xml

<activity android:name="qrcodeactivity" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="activity_a" />
</intent-filter>
</activity>


index.html

<a href="activity_a://qrcodeactivity">Activity A</a>


MyWebClient Java

private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

if (url.equals("activity_a://qrcodeactivity")) {
Intent intent = new Intent(getContext(), qrcodeactivity.class);
startActivity(intent);
return true; // Handle By application itself
} else {
view.loadUrl(url);

if (loader.equals("pull")) {
swipeContainer.setRefreshing(true);
} else if (loader.equals("dialog")) {
if (!pd.isShowing()) {
pd.show();
}
} else if (loader.equals("never")) {
Log.d("WebView", "No Loader selected");
}

return true;
}


}

@Override
public void onPageFinished(WebView view, String url) {
if (pd.isShowing()) {
pd.dismiss();
}

if (swipeContainer.isRefreshing()) {
swipeContainer.setRefreshing(false);
}
}

@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/" + getString(R.string.error_page));
}


}

Answer

WebView has no idea what activity_a:// is. Apparently, it is treating it as a relative reference, as if it were activity_a/.

Since you are using this in a WebView, there is no need to invent your own scheme. You are checking the entire URL in shouldOverrideUrlLoading().

So, you could change the HTML to:

<a href="/qrcodeactivity">Activity A</a>

and change your if to match:

if (url.equals("file:///qrcodeactivity")) {

And, you can get rid of the <intent-filter> from your <activity>. Having that is dangerous, anyway, as you are indicating that any app on the device can start that activity, as an activity with an <intent-filter> is exported.