1

I would like to make an activity be an option to open .json files type. For example, when I click on a .json file in my explorer, I want it to be opened (or to have an option to open it) in this activity. To achieve this, I have tried to add a pathPattern, a scheme and a host to my activity in the manifest. No success. The explorer says "No application to open this file." After searching on StackOverflow, I tried to add mimeType. Still no success. I've tried over 20 different answers from existing StackOverflow questions, without any success. Here's some of the answers I've tried: here, here, here. Here's my activity:

<activity
    android:name=".JsonActivity"
    android:label="Json">
    <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" />
        <data android:scheme="https" />
        <data android:scheme="content" />
        <data android:scheme="file" />
        <data android:host="*" />
        <data android:pathPattern=".*\\.json" />
    </intent-filter>
</activity>

What am I doing wrong? I made sure I added the WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions and also the category BROWSABLE and the action VIEW.

Vincent
  • 99
  • 2
  • 9
  • 2
    Do not shove everything into a single ``, particularly where the rules are incompatible. Your activity can have zero, one, or several `` elements. In your case, `content` does not always have file extensions, and neither `http` nor `https` are guaranteed to have them either. For those, you would need to use `mimeType`, not `host` and `pathPattern`. – CommonsWare Jun 22 '20 at 21:52
  • Thanks for the answer, it worked like a charm! What about files I don't know what the mimeType is? I'd like to be able to open .geojson files in my Actvitiy but I couldn't figure out what the mime type of .geojson files is. How can I achieve this? – Vincent Jul 02 '20 at 16:04
  • "What about files I don't know what the mimeType is?" -- in Android, you do not have great options for having such content pushed into your app via an ``. For GeoJSON, you could try `application/geo+json`, but I do not know how many apps will know about that MIME type. – CommonsWare Jul 02 '20 at 16:13
  • I have tried `application/geo+json`, `application/json` and `application/geojson` but sadly none of them work. Is there a workaround to open .geojson files in my Activity, instead of using intent filters? – Vincent Jul 02 '20 at 20:29
  • 1
    You can use `ACTION_OPEN_DOCUMENT` to request a document, and once again you can specify MIME types. I do not know whether the documents UI will recognize `application/geo+json` or not, meaning you may be stuck with `*/*` and need to validate that the user indeed chose a GeoJSON file. – CommonsWare Jul 02 '20 at 21:11

1 Answers1

0
You need multiple intent filters to address different situation you want to handle.

Example 1, handle http requests without mimetypes:

  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="http" />
    <data android:host="*" />
    <data android:pathPattern=".*\\.json" />
  </intent-filter>
Handle with mimetypes, where the suffix is irrelevant:

  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="http" />
    <data android:host="*" />
    <data android:mimeType="application/json" />
  </intent-filter>
Handle intent from a file browser app:

  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="file" />`enter code here`
    <data android:host="*" />
    <data android:pathPattern=".*\\.json" />
  </intent-filter>