Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to open an unknown file type

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){}
like image 237
rahul Avatar asked Oct 09 '22 00:10

rahul


1 Answers

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.

like image 158
Boris Strandjev Avatar answered Oct 13 '22 11:10

Boris Strandjev