【发布时间】:2021-07-11 06:54:41
【问题描述】:
我编写了一个代码,可以打开像 pdf 这样的文件。为此,我使用了如下意图:
public void openFile(Context context, File file) {
String typeFile = "application/pdf";
if (file.exists()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), typeFile);
PackageManager pm = context.getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setType(typeFile);
Intent openInChooser = Intent.createChooser(intent, "Choose");
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
if (resInfo.size() > 0) {
try {
context.startActivity(openInChooser);
} catch (Throwable throwable) {
Toast.makeText(context, "No application found which can open the file", Toast.LENGTH_SHORT).show();
// PDF apps are not installed
}
} else {
Toast.makeText(context, "No application found which can open the file", Toast.LENGTH_SHORT).show();
}
}
}
我的问题是,当我在 Android 10 和 11 上运行此代码时,else 中的 Toast 会执行,但在旧版本中这些代码可以正常工作。
那么我应该怎么做才能在 Android 10 和 11 中打开文件呢?
【问题讨论】:
标签: android file android-intent