TheFlash TheFlash - 4 months ago 9
Android Question

How to open file with any exetension in Android?

hello readers and helpers..!

(I m Newbie)


I have 1 app in which user can upload file with any exetention(.png,.mp3,.txt,.pdf,.bmp,.apk,...etc)

In a nutshell-->** the file with any exe..!**

These files r displayed in list-view.

When user 'Tap' or 'click' on item from list-view intent will be called (

) and it should be open the respective application which installed in device.

For Example

If User Click on nature.png then intent will be called and suggest the applications like Gallery,PS Touch..etc(As shown in screen shot1)

If User Click on book.pdf then intent will be called and suggest the applications like MuPdf,Polaris Office,apv..etc(As shown in screen shot2)

For .txt,.pdf,.png,.jpg-- I have solved the problem by set mime type
intent.setDataAndType(Uri.fromFile(file), mime);

and also for URL

Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(;

It is working perfectly but for rest of the file extension i m still confused-
What to do? and how to do?

I have already dig around,but couldn't get satisfactorily answer..!

My Questions:

  1. How can i write code for any file extensions so that it will call respective applications that installed in device??

  2. How to define custom file extensions in XML?


The following code will work for every file type:

try {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), fileMimeType);
} catch (ActivityNotFoundException e) {
    // no Activity to handle this kind of files

Of course, there should be an Activity in the system that knows how to handle the file of a certain type. Hope this helps.