views:

42

answers:

1

How do I create an activity which supports certain file types clicked in the web browser?

Here's what I tried:

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.sf.asap" android:versionCode="212" android:versionName="2.1.2">
    <application android:label="@string/app_name" android:icon="@drawable/icon">
        <activity android:name=".FileSelector" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Player" android:label="@string/playing">
            <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="file" android:host="*" />
                <data android:scheme="http" android:host="*" />
                <data android:scheme="https" android:host="*" />
                <data android:pathPattern=".*\\.sap" />
                <data android:pathPattern=".*\\.cmc" />
                <data android:pathPattern=".*\\.cm3" />
                <data android:pathPattern=".*\\.cmr" />
                <data android:pathPattern=".*\\.cms" />
                <data android:pathPattern=".*\\.dmc" />
                <data android:pathPattern=".*\\.dlt" />
                <data android:pathPattern=".*\\.mpt" />
                <data android:pathPattern=".*\\.mpd" />
                <data android:pathPattern=".*\\.rmt" />
                <data android:pathPattern=".*\\.tmc" />
                <data android:pathPattern=".*\\.tm8" />
                <data android:pathPattern=".*\\.tm2" />
            </intent-filter>
        </activity>
    </application>
</manifest> 

Fragment of Player.java:

Uri uri = getIntent().getData();
InputStream is = getContentResolver().openInputStream(uri);

When I enter an URL in the browser (Android 1.6) I get:

E/AndroidRuntime(12026): Caused by: java.lang.RuntimeException: java.io.FileNotFoundException: No content provider: http://asap.sourceforge.net/X_Ray_2.sap
E/AndroidRuntime(12026):    at net.sf.asap.Player.onCreate(Player.java:109)
E/AndroidRuntime(12026):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
E/AndroidRuntime(12026):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
+1  A: 

First, the following is an exceptionally bad idea:

            <data android:scheme="file" android:host="*" />
            <data android:scheme="http" android:host="*" />
            <data android:scheme="https" android:host="*" />

Your users will attack you with pickaxes if you interrupt every link they click on anywhere on their device so they have the option of handling that link with your application. Please remove these lines.

With respect to your error, you are attempting to open a Web URL (http://asap.sourceforge.net/X_Ray_2.sap) via getContentResolver().openInputStream(). That is not supported. A ContentResolver works with Android content providers. If you wish to download something off of the Internet, there are APIs for that, such as HttpClient.

CommonsWare
No, I filter by pathPattern and scheme/host are necessary just to make pathPattern work (apparently scheme="*" doesn't work). My application is presented to the user only for the extensions I listed (checked the web browser and OI File Manager).
getContentResolver().openInputStream() does work for "file:" URLs and I expected it to work with "http:" as well. Thank you!

related questions