如何在Android中打开.xlsx文件?

时间:2017-04-15 20:56:20

标签: java android excel mime xlsx

几个月前我写过app。我有Android Kitkat的Note 2(没关系)。我的朋友正在测试(现在他们正在使用它),一切都很好。

我用Android Nougat(7.0)购买了新手机。 现在我无法打开.xlsx文件。 单击按钮时我使用此方法。

private void ReadXLSX() {
    nameFILE= "OOOO.xlsx";
    Intent target = new Intent(Intent.ACTION_VIEW);
    target.setDataAndType(Uri.fromFile(new File((getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS))+"/"+ nameFILE)), "application/vnd.ms-excel");
    target.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Log.v("OPEN_FILE_PATH", getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/" + nameFILE);
   startActivity(target);

当我使用debbuger时: 那次事故发生后,我在这里

try {
                mResolvedMethod.invoke(mResolvedContext, v);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException(
                        "Could not execute non-public method for android:onClick", e);
            } catch (InvocationTargetException e) {
                throw new IllegalStateException(
                        "Could not execute method for android:onClick", e);
            }

我的AndroidManifest.xml有:

   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

------------- ------------- EDIT logcat的

 04-15 23:20:57.073 22020-22020/com.name.name E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.name.name, PID: 22020
                                                                           java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                               at android.view.View.performClick(View.java:5642)
                                                                               at android.view.View$PerformClick.run(View.java:22338)
                                                                               at android.os.Handler.handleCallback(Handler.java:751)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6209)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                            Caused by: java.lang.reflect.InvocationTargetException
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                               at android.view.View.performClick(View.java:5642) 
                                                                               at android.view.View$PerformClick.run(View.java:22338) 
                                                                               at android.os.Handler.handleCallback(Handler.java:751) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                               at android.os.Looper.loop(Looper.java:154) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6209) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
                                                                            Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.name.name/files/Download/OOOO.xlsx exposed beyond app through Intent.getData()
                                                                               at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
                                                                               at android.net.Uri.checkFileUriExposed(Uri.java:2346)
                                                                               at android.content.Intent.prepareToLeaveProcess(Intent.java:8933)
                                                                               at android.content.Intent.prepareToLeaveProcess(Intent.java:8894)
                                                                               at android.app.Instrumentation.execStartActivity(Instrumentation.java:1690)
                                                                               at android.app.Activity.startActivityForResult(Activity.java:4290)
                                                                               at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
                                                                               at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
                                                                               at android.app.Activity.startActivityForResult(Activity.java:4249)
                                                                               at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
                                                                               at android.app.Activity.startActivity(Activity.java:4588)
                                                                               at android.app.Activity.startActivity(Activity.java:4548)
                                                                               at com.name.name.MainActivity.OnlyReadFile(MainActivity.java:179)
                                                                               at com.name.name.MainActivity.onStaff(MainActivity.java:52)
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                               at android.view.View.performClick(View.java:5642) 
                                                                               at android.view.View$PerformClick.run(View.java:22338) 
                                                                               at android.os.Handler.handleCallback(Handler.java:751) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                               at android.os.Looper.loop(Looper.java:154) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6209) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

1 个答案:

答案 0 :(得分:0)

您必须在申请开始时询问权限

ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, 1);