Register to be able to open files of custom type. Say i have .cool files, and if the user tries to open it, Android asks if they would like to open it with my application. How?
4 Answers
You can add the following to the AndroidManifest.xml file inside the activity that has to open the file (pdf in our case)
<intent-filter >
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/pdf" />
</intent-filter>
Make sure you specify the proper mime format. The user will be prompted to choose your app if she wants to open the "pdf" file.
Check also Android intent filter: associate app with file extension for more details.
-
To open other kind of files you can change "application/pdf" to "*/*" (this will open all extension). To limit to a specific extension you can add – Xidh Oct 19 '17 at 13:09
-
And next... How app get path to file ??? – Lyusten Elder Jul 30 '18 at 06:19
I think that you don't choose that. This is handle by the system. Same thing when you send an intent to be handled by a map, If you have skyfire for instance, the system pops up a window an you can choose the application and choose if you want it to always open this kind of file.
Unless of course if your application is the only one to open this kind of file.
Edit
I think if you want your app to say "hey I'm able to open these .cool files", you need to set up <intent-filters>
with a tag <data>
and specificy the mimeType or Uri. Check here for more details.

- 50,022
- 30
- 123
- 131
-
2He is asking how to specify that his application can handle a filetype. – alexanderblom Aug 12 '10 at 07:20
-
The manifest part, and also starting the activity, and retrieving the file from someone selecting my application – Aymon Fournier Aug 12 '10 at 07:54
-
I don't have time now to right and test something. Try to do it please, I'll have a look later if I have time. – Sephy Aug 12 '10 at 08:05
-
I told you, I don't know right now, I have to run some tests and code. I'm not using that all the time. – Sephy Aug 12 '10 at 08:36
-
Did my advice help you dig in the right direction? Sorry I've been on the train all day... – Sephy Aug 12 '10 at 21:50
Here is a fuller example to show how to register your app to open plain text files.
Register the type of file you can handle
Add an intent-filter
to your activity in the manifest. In my case it is the ReaderActivity
that will display the text file. The mimeType
says that I will accept any plain text file. See this for specific file extensions.
<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>
Handle the intent
Get the data from the intent in your activity like this:
public class ReaderActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
handleIntent();
}
private void handleIntent() {
Uri uri = getIntent().getData();
if (uri == null) {
tellUserThatCouldntOpenFile();
return;
}
String text = null;
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
text = getStringFromInputStream(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (text == null) {
tellUserThatCouldntOpenFile();
return;
}
TextView textView = findViewById(R.id.tv_content);
textView.setText(text);
}
private void tellUserThatCouldntOpenFile() {
Toast.makeText(this, getString(R.string.could_not_open_file), Toast.LENGTH_SHORT).show();
}
public static String getStringFromInputStream(InputStream stream) throws IOException {
int n = 0;
char[] buffer = new char[1024 * 4];
InputStreamReader reader = new InputStreamReader(stream, "UTF8");
StringWriter writer = new StringWriter();
while (-1 != (n = reader.read(buffer))) writer.write(buffer, 0, n);
return writer.toString();
}
}
You get the data from the intent with getData()
to get the URI to the file. Then you use the content resolver to open an input stream. You use the content resolver because you might not have direct access to the file otherwise. Thanks to this answer for the method to convert the input stream to a string.

- 484,302
- 314
- 1,365
- 1,393
-
hi i have name my own extension. what should i put in mimeType? – Lorence Hernandez Dec 24 '18 at 13:10
-
@LorenceHernandez, check out [this link](https://stackoverflow.com/questions/1733195/android-intent-filter-for-a-particular-file-extension) for custom file extensions. – Suragch Dec 24 '18 at 16:20
-
The handleIntent part was what I needed, and it worked perfectly! Thank you! – ONE Aug 01 '19 at 20:39
One possible answer is shown here . Use this inside one of your activity tag and it'll be opened when the file have been pressed. I tested it using the native android file manager in Oreo.
<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>

- 2,165
- 2
- 15
- 22