Skip to content

Instantly share code, notes, and snippets.

@Logan676
Created August 5, 2015 02:05
Show Gist options
  • Save Logan676/37549fe53dea14040100 to your computer and use it in GitHub Desktop.
Save Logan676/37549fe53dea14040100 to your computer and use it in GitHub Desktop.
/**
* 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