How to associate Android app with Google Drive files having particular extension? - android

I need to associate files with particular extension from Google Drive (.gpx, for example) with my app. So when I click the .gpx file on Drive, I should have an option to open this file with my application.
Currently I have an activity handling these files clicking declared in AndroidManifest.XML in a way below, and it works for locally stored files and gmail attachments, but not for Drive:
<activity
android:name=".ui.PhotosActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:priority="1">
<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="ftp"/>
<data android:scheme="file"/>
<data android:host="*"/>
<data android:mimeType="*/*"/>
<data android:pathPattern=".*\\.gpx"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<action android:name="com.google.android.apps.drive.DRIVE_OPEN" />
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data
android:mimeType="application/octet-stream"
android:pathPattern=".*\\.gpx"
android:scheme="file"/>
<data
android:mimeType="application/octet-stream"
android:pathPattern=".*\\.gpx"
android:scheme="content"/>
</intent-filter>
</activity>

Add this to your manifest:
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
Then add this to your activity:
<meta-data
android:name="com.google.android.apps.drive.APP_ID"
android:value="id=xxxxxxx" />
where xxxxxxx is your Project ID as declared with Google API console.
<intent-filter>
<action android:name="com.google.android.apps.drive.DRIVE_OPEN" />
<data android:mimeType="application/vnd.google-apps.drive-sdk.1234567890" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.gpx" />
</intent-filter>

Related

My app with PROCESS_TEXT intent does not appear in all apps' copy-paste menu

I am trying to extend the copy/paste menu to open a specific activity in my app.
The problem is, that my app appears in some applications, and in most of them do not.
AndroidManifest.xml:
<manifest ....>
<queries>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT" />
<data android:mimeType="text/plain" />
</intent>
<intent>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="text/plain" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:mimeType="text/plain"/>
</intent>
</queries>
<application ...>
<activity
android:name=".component.popupActivity.PopUpActivity"
android:exported="true"
android:theme="#style/Theme.Transparent.SemiBlack">
<intent-filter>
<action android:name="android.intent.action.PROCESS_TEXT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<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:scheme="https" />
<data android:mimeType="text/html"/>
<data android:mimeType="text/plain"/>
<data android:mimeType="application/xhtml+xml"/>
</intent-filter>
</activity>
</application>
</manifest>
Is there anything wrong with my implementation?
I have tested it with WhatsApp, Instagram, Facebook, FB messenger.
Only works with Whatsapp.
Other apps like Translate, Outlook, idealo Shopping, Firefox Focus.
was always able to add their apps to the copy/paste menu.
Firefox Focus is open source, I did not notice anything different in AndroidManifest.xml
After researching, I found the following:
This intent-filter must be added to the activity.
I have tested it with Android 11 and Android 12.
AndroidManifest.xml:
<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" />
</intent-filter>

Android register app to open a custom extension file (also sharing it)

I am trying to let my app share and handle custom file extension and upon clicking it, it will open my app then I parse it.
I have looked into different ways like LIKE THIS for example and THIS, but clicking on the file from FileBrowser or WhatsApp for example is not detecting my app.
I am using the navigation component if it helps
I am not sure what I am doing wrong, I would appreciate it if someone have a working example.
Thanks.
This is some of the code I tried (I am testing with txt as it is an easy extension) (1)
<intent-filter
android:icon="#mipmap/ic_hp_launcher"
android:label="#string/app_name"
android:priority="999">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:host="*" />
<data android:pathPattern="*.txt" />
</intent-filter>
<intent-filter
android:icon="#mipmap/ic_hp_launcher"
android:label="#string/app_name"
android:priority="999">
<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/txt" />
</intent-filter>
I also tried (2)
<intent-filter
android:icon="#mipmap/ic_hp_launcher"
android:label="#string/app_name"
android:priority="999">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:host="*" />
<data android:pathPattern=".*\\.txt" />
</intent-filter>
<intent-filter
android:icon="#mipmap/ic_hp_launcher"
android:label="#string/app_name"
android:priority="999">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="*"
android:mimeType="*/*"
android:pathPattern=".*\\.txt"
android:scheme="file" />
</intent-filter>
<intent-filter
android:icon="#mipmap/ic_hp_launcher"
android:label="#string/app_name"
android:priority="999">
<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:scheme="content" />
<data android:mimeType="*/*" />
<!--
Work around Android's ugly primitive PatternMatcher
implementation that can't cope with finding a . early in
the path unless it's explicitly matched.
-->
<data android:host="*" />
<data android:pathPattern=".*\\.txt" />
<data android:pathPattern=".*\\..*\\.txt" />
<data android:pathPattern=".*\\..*\\..*\\.txt" />
<data android:pathPattern=".*\\..*\\..*\\..*\\.txt" />
<!-- keep going if you need more -->
</intent-filter>
and (3)
<intent-filter
android:icon="#mipmap/ic_hp_launcher"
android:label="#string/app_name"
android:priority="999">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:mimeType="text/plain" />
</intent-filter>
I finally my issue, I was testing multiple things at the same time.
I ended up using
<!-- this is needed for apps like whatsapp, it doesn't provide extension, so you can't add path -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.OPENABLE" />
<data
android:mimeType="application/octet-stream"
android:scheme="content" />
</intent-filter>
<!-- this is needed for apps like explorer -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.OPENABLE" />
<data
android:host="*"
android:mimeType="*/*"
android:pathPattern=".*\\.ext"
android:scheme="content" />
</intent-filter>
If you use mimeType="*/* then check logcat (no filter at all), search for content:// then you see what is the Android sending to your app, then modify the IntentFilter to match what you want. Some apps don't send the extension so using pathPattern was not working with it, Also using schema = content is needed
Thanks #CommonsWare for the help.

Android Open File Associations

I can open custom file extensions when accessing browser links without problem. How can I associate my app with file extensions locally on my device?
Specifically I would like to:
1) Open a file that has been downloaded - I drag the Notifications screen from the top of my display which lists recent .xm files that have been downloaded. I would like to be able to tap on those and have my application open these files.
2) Likewise with other file explorer apps if possible.
Here are my intent filters. What am I missing?
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:pathPattern=".*\\.xm" />
<data android:host="*" />
<data android:mimeType="text/plain"/>
</intent-filter>
<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:mimeType="*/*" />
<data android:pathPattern=".*\\.xm" />
<data android:host="*" />
</intent-filter>
UPDATE: See below filters
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:mimeType="application/octet-stream"
android:host="*"
android:pathPattern=".*\\.xm"
/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:mimeType="application/mytype"
android:host="*"
android:pathPattern=".*\\.xm"
/>
This post has helped me solved the problem. I'm including the required intent filters above in the original question.

android intent filter for custom file extension

I am trying to make an android app to open files with extension .abc and this is my application section from android manifest xml
<application android:label="#string/app_name" android:icon="#drawable/icon">
<activity android:name="com.example.app.ActName" android:label="AppName">
<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:mimeType="image/jpeg"/>
</intent-filter>
<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=".*\\.abc" />
<data android:host="*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
I used all the experience from the most popular and related questions
Android intent filter for a particular file extension?
Android intent filter: associate app with file extension
Android How to create Intent Filter for custom file extension that does NOT make it part of a chooser for everything on the phone
And in the end this does not work - when I click on a file named "name.abc" in ES file browser it asks me if i want to open the file as text or image etc. when it actually supposed to just open the file in my app instead
What am i doing wrong? What is the proper intent filter to launch the app by just clicking a file with the corresponding extension in any file manager?
upd.
it looks like different android systems and different file browsers act in different way - one intent filter works just fine on 4.2 in "ES File Explorer" and on 4.0.4 in default file manager, but does not work at all on 4.2 and 4.0.4 in "Total Commander" and on 4.0.4 in "ES File Explorer"
First of all, the first intent-filter in your example uses MIME type image/jpeg. Is jpeg your .abc extension?
I think there may be three reasons why this does not work:
You need to add more MIME types. Try adding the following:
<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:mimeType="application/abc"/>
</intent-filter>
<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:mimeType="application/octet-stream"/>
</intent-filter>
And, if your .abc format is a text format:
<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:mimeType="text/plain"/>
</intent-filter>
You have registered the file scheme. You might also need the content scheme:
<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="content" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.abc" />
<data android:host="*" />
</intent-filter>
Rumor has it that pathPattern with a dot only matches files containing a single dot. In your example, this would mean that your app would be associated with One.abc but not with Two.Dots.abc. Try adding more pathPatterns .*\\..*\\.abc, .*\\..*\\..*\\.abc etc.:
<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=".*\\..*\\.abc" />
<data android:host="*" />
</intent-filter>
if you try it in samsung my files it doesnt work so you have to do this
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
<data android:scheme="content" />
<data android:scheme="file" />
</intent-filter>
Because app is not done correctly, its samsung poorly coded my files app

how to make intent chooser for xml in android

i tried a lot of things, but i cant make it work, i had an application, and i want when there is an XML in the email, or maybe file explorer or more else, i want my app in the popup of the intent chooser, i cant make it work, anyone has a clue?
btw lets say i make it work, so how i can "handle" when people choose my app in onCreate() to load the information.
here its part of my app i tried a lot of things
<i><application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
<data android:mimeType="application/xml"/>
<data android:scheme="http" android:host="*"
android:pathPattern=".*xml" />
</intent-filter>
</activity>
</application> </i>
Try being a little more broad with your filter, e.g.
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/xml"/>
<data android:scheme="content" android:mimeType="text/*" android:pathPattern=".*\\.xml"/>
<data android:scheme="file" android:mimeType="text/*" android:pathPattern=".*\\.xml"/>
<data android:scheme="http" android:mimeType="text/*" android:pathPattern=".*\\.xml"/>
<data android:scheme="https" android:mimeType="text/*" android:pathPattern=".*\\.xml"/>
</intent-filter>
Other thing you could do is catch the intents in broadcast receiver and rebroadcast as neededed.
this is the code i use, i figure out i need to create another intent filter for view
<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:mimeType="application/xml" />
<data android:mimeType="text/xml" />
<data android:mimeType="text/*" />
</intent-filter>

Categories

Resources