5

I am developing a file explorer app in android. How to handle files with unknown extensions? When I try to open such kind of file, its throwing ActivityNotFound exception. But I want the system to pop up list of apps so that we can manually choose an application to open it. Can anyone help me here?

I am starting activity to open the file by binding the file and its extension to the intent.

Intent intent = new Intent(Intent.ACTION_VIEW);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
intent.setDataAndType(Uri.fromFile(new File(file.toString())), type);
try
{
    startActivity(intent);
}
catch(Exception e){}
Xavi
  • 20,111
  • 14
  • 72
  • 63
rahul
  • 6,447
  • 3
  • 31
  • 42

2 Answers2

4

ActivityNotFound is thrown when no application is registered that can handle specific file type. This means that the list of apps you want to show will be empty.

The most appropriate way to deal with he situation is to catch ActivityNotFound exception and show a toast notifying the user there are no appropriate applications to open the file.

All android browsers proceed in this manner.

Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • Asus File Manager (4.4) give's a generic "Open as" list with four items representing generic categories (Text, Image, Audio, Video). Selecting one will give a list of associated apps to open the file with (much like Windows). – samus Jul 12 '17 at 13:48
0

I will leave this link here, that targets the same problem and has a little more detail to it (second answer, read comments): Launching an Activity based on a file in android

Community
  • 1
  • 1
mindandmedia
  • 6,800
  • 1
  • 24
  • 33