Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Attempts to open a file for viewing.
*
* @param fileholder The holder of the file to open.
*/
public static void openFile(FileHolder fileholder, Context c) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = FileUtils.getUri(fileholder.getFile());
String type = fileholder.getMimeType();
if ("*/*".equals(type)){
intent.setData(data);
intent.putExtra(FileManagerIntents.EXTRA_FROM_OI_FILEMANAGER, true);
} else {
intent.setDataAndType(data, type);
}
try {
List<ResolveInfo> activities = c.getPackageManager().queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);
if (activities.size() == 0 || (activities.size() == 1 && c.getApplicationInfo().packageName.equals(activities.get(0).activityInfo.packageName))){
Toast.makeText(c, R.string.application_not_available, Toast.LENGTH_SHORT).show();
return;
} else {
c.startActivity(intent);
}
} catch (ActivityNotFoundException e) {
Toast.makeText(c.getApplicationContext(), R.string.application_not_available, Toast.LENGTH_SHORT).show();
} catch (SecurityException e){
Toast.makeText(c.getApplicationContext(), R.string.application_not_available, Toast.LENGTH_SHORT).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.