Tim Tolparov
Tim Tolparov

Reputation: 79

How to add my app to "Open With" dialog?

I need my Android app to be in Open With dialog for SQLite files.

Like when you install new web-browser it will appears in Open With dialog for html-files.

How can I do that?

Upvotes: 1

Views: 4413

Answers (2)

Tim Tolparov
Tim Tolparov

Reputation: 79

This answer I found in Russian StackOverflow : https://ru.stackoverflow.com/a/420927/180697

<activity name="com.your.activity">
<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" />
    <data android:mimeType="*/*" />
    <data android:pathPattern=".*\\.sqlite" />
</intent-filter>

This is what you need to add to your Activity class:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    final Intent intent = getIntent();  
    final String action = intent.getAction();

    if(Intent.ACTION_VIEW.equals(action)){
        Uri uri = intent.getData();
        new File(uri.getPath()); //дальше делаем все, что надо с файлом 
    } else {
        Log.d(TAG, "intent was something else: "+action);
    }
}

So I only need to understand what to write in activity!)) Thanks!

Upvotes: 4

Dave Durbin
Dave Durbin

Reputation: 3642

To appear in the 'Open With' dialog, your Android app must declare in its manifest that it handles a particular intent and then specify the mime-type of the file in the intent. For example :

<intent-filter >
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="application/x-sqlite" />
</intent-filter>

Note that the mime type for SQLite may not be recognised as I don't think this is yet a standard. You may want to use application/octet-stream instead and then in your own code, double check that the file being provided is actually a valid SQLite file (which you should do anyway).

You can find more information on tags here and on intent-filters in general here

Upvotes: 3

Related Questions