I have my android app, that downloads a file via the DownloadService. This file is in special file format, that only can be used in my app - it has the suffix *.foobar.
I start the download this way:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDestinationInExternalPublicDir("/", "aaaa.foobar");
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Download works fine, but when I tap on the notification "download finished", I want that my activity will be opened. What I get is the toast "File can not be opened".
In my Manifest I have a intent-filter for the .foobar file suffix.
<intent-filter android:label="#string/app_name">
<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=".*\\.foobar" />
<data android:host="*" />
</intent-filter>
Am I missing something?
finally found the solution!
I had to remove the path pattern line
<data android:pathPattern=".*\\.foobar" />
and change the schema to content. So the complete intent filter is:
<intent-filter android:label="#string/app_name">
<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="application/wcmap" />
<data android:host="*" />
</intent-filter>
I think you will find the answer to this answer. Most likely you have a problem in the treatment of Activiti or slightly incorrect description of intent.
Try adding a priority.
android:priority="integer"
Also you should look:
Android intent filter for a particular file extension?
Android intent filter for custom file extension
Good luck. Sorry that can not help more. Too much text for a comment.
Related
I'm creating a custom intent filter which will open my app when I tap a file with a certain file extension (.myextension in this case.) The intent-filter is below. This currently works perfectly every time I open a .myextension file from my emulator. However, when I try it from my device, it no longer works.
On my device, when I tap a .myextension file in the Files browser app, I see Unable to preview file. instead of automatically opening the app. I've tried opening the file from quite a few locations (Files app, GDrive, Downloads folder, Slack/Gmail, both internal storage and SDCard.) I'm using Android 10 on both my emulator and my device.
<intent-filter android:label="My App">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:scheme="content"
android:mimeType="*/*"
android:host="*"
android:pathPattern=".*\\.myextension" />
</intent-filter>
I've also tried replacing that data tag/adding a second tag with this block but it doesn't seem to help:
<data
android:scheme="file"
android:mimeType="*/*"
android:host="*"
android:pathPattern=".*\\.myextension" />
Am I missing something obvious? Any help would be greatly appreciated. Thanks!
This currently works perfectly every time I open a .myextension file from my emulator.
It will fail much of the time, as it is not really tied to an emulator versus a device.
However, when I try it from my device, it no longer works.
A content Uri is not required to have a file extension, just as an https URL is not required to have a file extension. Much of the time, a content Uri will not have a file extension, and such a Uri will not match your <intent-filter>.
ACTION_VIEW is mostly for files with a widely-recognized MIME type.
I've also tried replacing that data tag/adding a second tag with this block but it doesn't seem to help
file Uri values have been generally banned since Android 7.0.
<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:pathPattern=".*\\.myextension" />
<data android:pathPattern="/*.*\\.myextension" />
<data android:pathPattern="/*.*\\..*\\.myextension" />
<data android:pathPattern="/*.*\\..*\\..*\\.myextension" />
<data android:pathPattern="/*.*\\..*\\..*\\..*\\.myextension" />
<data android:pathPattern="/*.*\\..*\\..*\\..*\\..*\\.myextension" />
<data android:pathPattern="/*.*\\..*\\..*\\..*\\..*\\..*\\.myextension" />
<data android:pathPattern="/*.*\\..*\\..*\\..*\\..*\\..*\\..*\\.myextension" />
<data android:pathPattern="/*.*\\..*\\..*\\..*\\..*\\..*\\..*\\..*\\.myextension" />
<data android:host="*" />
</intent-filter>
Though it doesnt work in some of the file managers Better first try in ESExplorer.
I am saving a binary to a file with a custom extension for instance .custom. How do I save it to a specific mime type? I want my app to be called to open that custom file. In the manifest I used / as mimeType but the app gets called even when tapping an image. I used octet-stream but the file doesn't get recognized and the app does not get opened. I just want to save a binary with custom extension and the OS would call my app when it encounters this file.
<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="application/octet-stream" />
<data android:pathPattern=".*\\.test" />
<data android:host="*" />
</intent-filter>
Finally, after several views only without answers I figured it out. The post on this link helped me resolve my issue.
https://publish.illinois.edu/weiyang-david/2013/04/11/how-to-write-intent-filter-dealing-with-mime-type-problem/
This example was it
<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=".*\.pdf" />
</intent-filter>
I replaced the scheme to "file" and replaced with my pathPattern. Removing the mimeType was key to solving my issue. File browsers now suggests to open the custom file with my app.
Want to create an android application, which opens a custom-build file extension (for example, I want to open .abcd files)
It is something like Adobe Reader that opens .pdf files, or Photo Viewer that opens .jpg files
Specific conditions:
1. The .abcd file should be outside / external from the application itself. (as .pdf is to Adobe Reader)
2. The .abcd file would be a zipped file, which contains few folders and .xml, .txt, and .jpg files. I think I want to extract it - maybe temporarily - to somewhere in the storage (definitely need a zipper/unzipper library), then read the individual .xml, .txt, and .jpg files.
Looking for insights and answers for this problem.
Additional information:
I am relatively new to Android programming.
I think you need to do that type of customization via intent-filter something like:
<intent-filter android:icon="your_drawable-resource"
android:label="your_string_resource"
android:priority="integer">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:host="*" />
<data android:pathPattern=".*\\.YOUR_CUSTOM_FILE_EXTENSION" />
</intent-filter>
Also you should look:
Custom Filetype in Android not working
Android intent filter for a particular file extension?
android intent filter for custom file extension
One possible answer is shown
here
. Try some customisation for intent filters.
<intent-filter android:priority="999">
<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="*" />
<data android:mimeType="application/octet-stream" />
<data android:pathPattern=".*\\..*\\..*\\..*\\..*\\.yourextension" />
<data android:pathPattern=".*\\..*\\..*\\..*\\.yourextension" />
<data android:pathPattern=".*\\..*\\..*\\.yourextension" />
<data android:pathPattern=".*\\..*\\.yourextension" />
<data android:pathPattern=".*\\.yourextension" />
<data android:scheme="content" />
</intent-filter>
I added an intent filter to my activity for a specific type of mime and extension, so when I click on that file it opens the activity that is supposed to open.
Thats awesome, and its working!
But how can I get the file that I clicked?
I guess that its possible.
Can someone give me some hints?
Thanks alot in advance ;)
EDIT:
I Have this on my AndroidManifest.xml
<activity
android:name="com.activities.omega1.SlaveLoadConfig"
android:label="#string/title_activity_slave_load_config"
android:screenOrientation="portrait" >
<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=".*\\.sso" />
<data android:host="*" />
</intent-filter>
</activity>
And I already have files with extension .sso opening the SlaveLoadConfig activity, but I would like to have an handler to get the content of the file, how ?
Found the solution :P
Intent intent = getIntent();
intent.getData().toString();
I am making an app that is able to upload single or multiple files or folders. The intent-filter is defined like this:
<activity android:name=".UploadActivity" android:launchMode="singleTop" android:theme="#style/Theme.Sherlock">
<intent-filter android:label="#string/upload_intent">
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="application/*" />
<data android:mimeType="audio/*" />
<data android:mimeType="image/*" />
<data android:mimeType="media/*" />
<data android:mimeType="multipart/*" />
<data android:mimeType="text/*" />
<data android:mimeType="video/*" />
</intent-filter>
<intent-filter android:label="#string/upload_intent">
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>
This works great with Blackmoon File Explorer and the Android 4.0.3 Gallery. My app shows up in the Share menus. However, it also shows in the Browser, when you long-press the URL and choose to Share Page.
When I send a text file to my app, I get this:
getIntent().getAction() returns Intent.ACTION_SEND
getIntent().getType() returns "text/plain"
getIntent().getScheme() returns null
getIntent().getExtras().getString(Intent.EXTRA_STREAM) returns the filename
However, sending a URL from the Browser returns the same thing minus the Intent.EXTRA_STREAM. I can detect this easily in code and say "hey I can't upload text!" but I'd rather change the intent filter so that it only triggers when there is an EXTRA_STREAM. Is it possible to do so?
(I tried using android:scheme but that won't differentiate between a file and a text string being shared, since they are both null...)
You have:
getIntent().getExtras().getString(Intent.EXTRA_STREAM) returns the filename
AFAIK, Android never sends you an open fh or socket to a file. Just the plain, old path to the file, which you then handle with:
File somefile = new File(filename);
And then read it or write to it or what not.
As for the incoming text being EXTRA_TEXT or STREAM, just test for both and handle appropriately. Hope this helps.
I registered for file in the manifest:
<activity android:name=".ReaderActivity">
<intent-filter >
<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>
</activity>
And then used getData() and a content resolver in my activity.
Uri uri = getIntent().getData();
InputStream inputStream = getContentResolver().openInputStream(uri);
...
See this answer to find out what the ... is.