Search code examples
androidbrowserhrefintentfilter

Open file link from android browser in my app


I want to be able to run the standard android browser in the usual way, navigate to a page with a link to a file on it, eg

<a href=./myfile.gcsb>click here</a>

click the link and have it start my app, which can then process the file as necessary.

The webpage basically works OK - click on the link from Firefox on a PC and as expected I get the options to save the file, or to open it with the registered app for .gcsb file types. But not on the android device.

I've tried every version of an intent-filter in my app that I can think of however, for instance

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="http" android:host="*" android:mimeType="application/*" android:pathPattern=".*\\.gcsb" />
</intent-filter>

and none of them cause the app to be started on clicking the link in the browser.

I know there are loads of posts about this, but none of them have helped me solve it so far. Any suggestions gratefully received. (Running Android OS 2.2).


Solution

  • Sorted it. Firstly from other posts it appears that there is generally a problem opening links from the android browser in the same window. Therefore adding target=_blank to the original

    <a href ...> ...
    

    dealt with that (though see proviso at the end).

    The intent-filter that appears to work is

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="http" android:host="*" android:pathPattern=".*\\.gcsb" />
    </intent-filter>
    

    and the code in the activity onCreate() / onRestart() is

    String  action = intent.getAction();
    
    if (!Intent.ACTION_VIEW.equals(action))
        return;
    
    Uri     uri = intent.getData();
    String  scheme = uri.getScheme();
    String  name = null;
    
    if (scheme.equals("http")) {
        List<String>
                pathSegments = uri.getPathSegments();
    
        if (pathSegments.size() > 0)
            name = pathSegments.get(pathSegments.size() - 1);
    
        URL     url = new URL(uri.toString());
        HttpURLConnection
                urlConnection = (HttpURLConnection)url.openConnection();
    
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();
    
        is = urlConnection.getInputStream();
    }
    
    else if () {
        //  Deal with other schemes (file, content) here
    }
    
    //  Should have an open input stream and a target file name/ext by here
    //  Make full path to where it needs to be saved from name
    //  Open output stream
    //  Loop round reading a buffer full of data from input stream and writing to output stream
    //  Close everything
    //  Not forgetting to deal with errors along the way
    

    This all appears to work, with two provisos: (1) all this should really be done in a background thread, and (2) the effect of target=_blank is to get the android browser to keep opening new windows everytime a download is done - and there is a limit to how many it will open.