Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open file with any exetension in Android?

Tags:

android

I have an 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 (android.content.Intent.ACTION_VIEW) 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(https://www.google.co.in/));

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?
like image 735
TheFlash Avatar asked Feb 21 '13 11:02

TheFlash


People also ask

How do I open a file type on Android?

File Viewer is a FREE Android app that allows you to open and view files on your Android device. It supports over 150 file types and can display the contents of any file. You can use File Viewer's information panel to view hidden file details and metadata. Get File Viewer FREE from the Google Play store!

How do you change the extension of a file on Android?

Press this file long and press the three vertical dots at the top. Tap “Rename,” and in the popup, you'll see the file name, the dot, and the extension. Remove the existing extension and then enter the desired extension. Press the “OK” button and tap “Rename.”


1 Answers

The following code will work for every file type:

try {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), fileMimeType);
    startActivity(intent);
} 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.

like image 158
Egor Avatar answered Oct 11 '22 04:10

Egor