Stack Overflow archive
1 scoreaccepted

.puz file picker- Android

score
1
question views
343
license
CC BY-SA 3.0

Add the following intent-filter to the relevant activity tag in your AndroidManifest:

xml
<intent-filter>
  <action android:name="android.intent.action.VIEW"/>

  <category android:name="android.intent.category.DEFAULT"/>

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

  <data android:pathPattern="/.*\\.puz"/>
  <data android:pathPattern="/.*\\.PUZ"/>
</intent-filter>

Add the following code in onCreate(Bundle) to check if the Activity was started from another app:

java
if (getIntent().getData() != null) {
  String path = getIntent().getData().getPath();
  if (path != null && path.toLowerCase(Locale.ENGLISH).endsWith(".puz")) {
    File puz = new File(path);
    // TODO: do stuff with puz
  }
}

Note: File managers might use different methods for finding apps that can handle a certain intent/file. If your app doesn't show in a certain file manager, try tweaking your intent-filter. For example, you might need to add:

xml
<data android:pathPattern="/.*\\..*\\.puz"/>
<data android:pathPattern="/.*\\..*\\..*\\.puz"/>
<data android:pathPattern="/.*\\..*\\..*\\..*\\.puz"/>
<data android:pathPattern="/.*\\..*\\..*\\..*\\..*\\.puz"/>
<data android:pathPattern="/.*\\..*\\..*\\..*\\..*\\..*\\.puz"/>

For paths that include more dots or are in hidden directories. (see here)

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.