Android Webview ERR_UNKNOWN_URL_SCHEME Error

25,881

Solution 1

try this

if(url.startsWith("mailto:")){
        MailTo mt = MailTo.parse(url);
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("text/plain");
        i.putExtra(Intent.EXTRA_EMAIL, new String[]{mt.getTo()});
        i.putExtra(Intent.EXTRA_SUBJECT, mt.getSubject());
        i.putExtra(Intent.EXTRA_CC, mt.getCc());
        i.putExtra(Intent.EXTRA_TEXT, mt.getBody());
        mContext.startActivity(i);
        view.reload();
        return true;
    } 

Solution 2

This error is appeared because the WebView can’t recognize the URL Scheme,for example, the WebView will usually recognize http and https, anything other than these, for example – intent://,market://,app://,mail:// etc will not be recognized by webview unless we add a handler to handle these url schemes or by disabling these schemes and only load http and https schemes.

Here is an example to fix the common intent url scheme.

mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return loadUrl(view, url);
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}

@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}

@Override
public void onReceivedError(WebView view, int errorCode, String description,String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
//Log.d("Web Request Error", "");
showError(errorCode);
}

@TargetApi(Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
Log.d("WebResource error", "");
int errorCode = error.getErrorCode();
showError(errorCode);
}
});

private boolean loadUrl(WebView view, String url) {
if (url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
return false;
}
// Otherwise allow the OS to handle it
else if (url.startsWith("tel:")) {
Intent tel = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(tel);
return true;
} else if (url.toLowerCase().startsWith("mailto:")) {
MailTo mt = MailTo.parse(url);
Intent emailIntent = newEmailIntent(mt.getTo(), mt.getSubject(), mt.getBody(), mt.getCc());
startActivity(emailIntent);
return true;
}
return true;
}
Share:
25,881
Dauezevy
Author by

Dauezevy

Updated on October 22, 2021

Comments

  • Dauezevy
    Dauezevy over 2 years

    When I click a link which goes to mailto:[email protected] I get this error:

    net: ERR_UNKNOWN_URL_SCHEME

    I tried to add an if(url.startsWith("mailto:")) condition but it's not working.

    This is my MyWebViewClient method:

    public class MyWebViewClient extends WebViewClient {
    
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
    
            }
    
            @Override
            public void onPageFinished(WebView view, String url) {
                view.setVisibility(View.VISIBLE);
                final Animation fade = new AlphaAnimation(0.0f, 1.0f);
                fade.setDuration(200);
                view.startAnimation(fade);
                view.setVisibility(View.VISIBLE);
    
            }
    
        }
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.startsWith("mailto:")){
                Intent intent = null;
                try {
                    intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
                view.getContext().startActivity(intent);
            }
            else if (url.endsWith(".mp3")) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.parse(url), "audio/*");
                startActivity(intent);
    
            } else if (url.endsWith(".mp4") || url.endsWith(".3gp")) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.parse(url), "video/*");
                startActivity(intent);
            }
            else {
                return false;
            }
            view.reload();
            return true;
        }
    

    and this is how I add the function to my web view before loadUrl:

    ...
    mWebview.setWebViewClient(new MyWebViewClient());
    ...
    
  • sulai
    sulai about 9 years
    This works great, but if you want the intent to target email applications only, use intent.setType("message/rfc822");. Also see stackoverflow.com/questions/4788461/handling-links-in-a-webv‌​iew
  • Admin
    Admin over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.