我试图在我的安卓应用程序中打开一个XLSX文件。
我知道我必须启动的Intent类型是application/excel,但是尽管我已经安装了Google Sheets,但是我的代码说没有一个应用程序可以打开我的excel文件。
这是我用来触发Intent的代码
private void openXLS(){
File xls = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "prova.xlsx");
Uri path = Uri.fromFile(xls);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/excel");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "No Application available to view XLS", Toast.LENGTH_SHORT).show();
}
}注: prova.xlsx存在,我能够到达并打开它。
发布于 2015-05-09 06:21:37
解决了
使用MIME类型可以打开application/vnd.ms-excel、*.xls和*.xlsx文件。
发布于 2019-04-03 09:54:43
private void openXLS(final String path) {
File file = new File(path);
Uri uri ;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
} else {
uri = Uri.fromFile(file);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/vnd.ms-excel");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Application not found", Toast.LENGTH_SHORT).show();
}
}https://stackoverflow.com/questions/30118799
复制相似问题