我需要做类似的事情,发现这个答案很有帮助。我已经对其进行了修改,使其成为一个完整的解决方案:
public static void openFileWithInstalledAppExceptCurrentApp(File file, final Activity activity) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = file.getName().substring(file.getName().indexOf(".")+1);
String type = mime.getMimeTypeFromExtension(ext);
intent.setDataAndType(Uri.fromFile(file),type);
PackageManager packageManager = activity.getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
String packageNameToHide = "com.test.app";
ArrayList<Intent> targetIntents = new ArrayList<Intent>();
for (ResolveInfo currentInfo : activities) {
String packageName = currentInfo.activityInfo.packageName;
if (!packageNameToHide.equals(packageName)) {
Intent targetIntent = new Intent(android.content.Intent.ACTION_VIEW);
targetIntent.setDataAndType(Uri.fromFile(file),type);
targetIntent.setPackage(packageName);
targetIntents.add(targetIntent);
}
}
if(targetIntents.size() > 0) {
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "Open file with");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[] {}));
activity.startActivity(chooserIntent);
}
else {
Toast.makeText(this, "No app found", Toast.LENGTH_SHORT).show();
}
}