Intent.createChooser()
我使用方法打开选择器对话框。但在那个对话框中,我看不到“仅一次”和“总是”选项。Intent.createChooser()
我想要这种类型的带有方法的选择器对话框
这是我的代码。
File mFile = new File("give your file path here");
String type = "video/mp4";
Intent viewDoc = new Intent(Intent.ACTION_VIEW);
viewDoc.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
viewDoc.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(this, "com.xyz.fileprovider", mFile);
viewDoc.setDataAndType(contentUri, type);
PackageManager pm = getPackageManager();
apps = pm.queryIntentActivities(viewDoc, PackageManager.MATCH_DEFAULT_ONLY);
ArrayList<Intent> targetIntents = new ArrayList<Intent>();
for (ResolveInfo resolveInfo : apps) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (!BuildConfig.APPLICATION_ID.equals(packageName)) {
Intent targetIntent = new Intent(Intent.ACTION_VIEW); // targetIntent.setDataAndType(contentUri, type);
targetIntent.setPackage(packageName);
targetIntent.setDataAndType(contentUri, type);
targetIntents.add(targetIntent);
}
}
if (type == null || type.equals("") || targetIntents.size() <= 0) { //application/octet-stream
android.app.AlertDialog alertDialog = new android.app.AlertDialog.Builder(this).create();
alertDialog.setTitle(getResources().getString(R.string.app_name));
alertDialog.setMessage(getResources().getString(R.string.offline_file_not_supported_error));
alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
} else {
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "Open file with");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
//startActivity(viewDoc);
}
但我需要在这个对话框中只需要一次和总是选项。
我使用了下面提到的代码,因为我想从选择器中删除我自己的应用程序。
for (ResolveInfo resolveInfo : apps) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (!BuildConfig.APPLICATION_ID.equals(packageName)) {
Intent targetIntent = new Intent(Intent.ACTION_VIEW); // targetIntent.setDataAndType(contentUri, type);
targetIntent.setPackage(packageName);
targetIntent.setDataAndType(contentUri, type);
targetIntents.add(targetIntent);
}
}