简体   繁体   English

无法使用Intent.ACTION_VIEW在Android上打开Excel文件

[英]Unable to open excel file on Android using Intent.ACTION_VIEW

I'm trying to let a user export an excel file in my app and have the app automatically open the created excel file. 我试图让用户在我的应用程序中导出一个excel文件,并让该应用程序自动打开创建的excel文件。 The excel file is created and stored successfully using jxl, but when I try to open it with the Hancom Office Editor nothing happens other than my screen getting a dark overlay. 使用jxl成功创建并存储了excel文件,但是当我尝试使用Hancom Office编辑器打开它时,除了屏幕变暗之外没有其他反应。 Here's a gif: 这是一个gif:
在此处输入图片说明
I can't figure out what would cause this and can't find anything about it online. 我不知道是什么原因造成的,也无法在线找到任何相关信息。 I'm using the accepted answer from here to support my target SDK 28. 我正在使用此处接受的答案来支持我的目标SDK 28。

my export method: 我的导出方法:

 public void onExport(View view){
        try {
            //write changes to excel file(changes made outide of function)
            workbook.write();
            workbook.close();
            Toast.makeText(getApplication(),
                    "Data Exported to Excel Sheet", Toast.LENGTH_SHORT).show();
            findViewById(R.id.btn_export).setEnabled(false);

            //set file to saved excel file
            File file = new File(Environment.getExternalStorageDirectory(),
                    race.getName()+".xls");
            Uri path = FileProvider.getUriForFile(getApplicationContext(), "com.something.racecalculator.fileprovider", file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            Log.i("path: ", path.toString());
            intent.setDataAndType(path, "image/jpg");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
        catch (ActivityNotFoundException e) {
            Log.i("oh no:", "No application available to view excel");
        }
 }

My provider tag in AndroidManifest.xml(set as a child of ): 我在AndroidManifest.xml中的提供者标记(设置为的子代):

<provider
            android:name=".GenericFileProvider"
            android:authorities="com.something.racecalculator.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

I'm not getting any Errors or warnings as far as I can tell. 据我所知,我没有收到任何错误或警告。 Thanks for any help. 谢谢你的帮助。

I'm not sure exactly what caused the problem, however, I suspect that I might have had the file path wrong. 我不确定到底是什么引起了问题,但是,我怀疑文件路径可能错误。 I changed a couple things (including where the excel files were being saved to. Changed from /storage/emulated/0/ to /storage/emulated/0/Samsung ) and here is what worked: 我做了几件事(包括将excel文件保存到的位置。从/storage/emulated/0/更改为/storage/emulated/0/Samsung ),这是可行的:

Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File("/storage/emulated/0/Samsung"+File.separator + race.getName()+".xls");
Uri path = FileProvider.getUriForFile(getApplicationContext(), "com.something.racecalculator.fileprovider", file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(path, "application/vnd.ms-excel");
startActivity(intent);

remember that you need to implement a FileProvider if your target SDK is 24 or above 请记住,如果您的目标SDK为24或更高版本,则需要实现FileProvider

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM