Excel文件不使用Apache存储内部存储器

时间:2022-03-25 16:37:09

I creating Excel file using apache.i am getting error like Permission denied but i add permission in my manifest file.

我使用apache.i创建Excel文件我收到错误,如Permission denied,但我在我的清单文件中添加权限。

In the similar way i create for pdf it's working.

以我为pdf创建的类似方式,它正在工作。

I refered this tutorial:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/

我参考了这个教程:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/

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

My java code below:

我的java代码如下:

buttonone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createfile();
            }
        });

private void createfile()
    {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Datatypes in Java");
        Object[][] datatypes = {
                {"Datatype", "Type", "Size(in bytes)"},
                {"int", "Primitive", 2},
                {"float", "Primitive", 4},
                {"double", "Primitive", 8},
                {"char", "Primitive", 1},
                {"String", "Non-Primitive", "No fixed size"}
        };

        int rowNum = 0;
        System.out.println("Creating excel");

        for (Object[] datatype : datatypes) {
            Row row = sheet.createRow(rowNum++);
            int colNum = 0;
            for (Object field : datatype) {
                Cell cell = row.createCell(colNum++);
                if (field instanceof String) {
                    cell.setCellValue((String) field);
                } else if (field instanceof Integer) {
                    cell.setCellValue((Integer) field);
                }
            }
        }

        try {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/IDEA FILES/"+"MyFirstExcel.xlsx";
            File dir = new File(path);

            if (!dir.exists())
                dir.mkdirs();

            FileOutputStream fOut = new FileOutputStream(dir);
            workbook.write(fOut);
            fOut.flush();
            fOut.close();
            workbook.close();

            //viewExecel()
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.d("getpermission","**   "+e.toString());
        } catch (IOException e) {
            e.printStackTrace();

            Log.d("getpermission","**ioexeption   "+e.toString());
        }

        System.out.println("Done");


    }

Run time Error:

运行时错误:

09-20 11:38:36.544 20869-20869/pro.kondratev.xlsxpoiexample W/System.err: java.io.FileNotFoundException: /storage/emulated/0/IDEA FILES/MyFirstExcel.xlsx: open failed: EACCES (Permission denied)
09-20 11:38:36.553 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:452)
09-20 11:38:36.553 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
09-20 11:38:36.553 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
09-20 11:38:36.553 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at pro.kondratev.xlsxpoiexample.MainActivity.createfile(MainActivity.java:93)
09-20 11:38:36.553 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at pro.kondratev.xlsxpoiexample.MainActivity.access$000(MainActivity.java:33)
09-20 11:38:36.553 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at pro.kondratev.xlsxpoiexample.MainActivity$1.onClick(MainActivity.java:52)
09-20 11:38:36.553 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at android.view.View.performClick(View.java:5201)
09-20 11:38:36.553 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at android.view.View$PerformClick.run(View.java:21163)
09-20 11:38:36.553 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at android.os.Handler.handleCallback(Handler.java:746)
09-20 11:38:36.553 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
09-20 11:38:36.553 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at android.os.Looper.loop(Looper.java:148)
09-20 11:38:36.553 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5443)
09-20 11:38:36.553 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-20 11:38:36.553 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
09-20 11:38:36.553 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-20 11:38:36.553 20869-20869/pro.kondratev.xlsxpoiexample W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
09-20 11:38:36.555 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at libcore.io.Posix.open(Native Method)
09-20 11:38:36.555 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
09-20 11:38:36.555 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:438)
09-20 11:38:36.556 20869-20869/pro.kondratev.xlsxpoiexample W/System.err:   ... 14 more
09-20 11:38:36.556 20869-20869/pro.kondratev.xlsxpoiexample D/getpermission: **   java.io.FileNotFoundException: /storage/emulated/0/IDEA FILES/MyFirstExcel.xlsx: open failed: EACCES (Permission denied)

Please help in this case..Thanks in advance.

在这种情况下请帮忙..谢谢。

1 个解决方案

#1


0  

i think you need to ask run time permssion becase Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.

我认为你需要询问运行时间因为在Android 6.0(API级别23)开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时。

code

String permission = Manifest.permission.WRITE_INTERNAL_STORAGE;
int grant = ContextCompat.checkSelfPermission(this, permission);
    if (grant != PackageManager.PERMISSION_GRANTED) {
         String[] permission_list = new String[1];
         permission_list[0] = permission;
         ActivityCompat.requestPermissions(this, permission_list, 1);
    }

#1


0  

i think you need to ask run time permssion becase Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.

我认为你需要询问运行时间因为在Android 6.0(API级别23)开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时。

code

String permission = Manifest.permission.WRITE_INTERNAL_STORAGE;
int grant = ContextCompat.checkSelfPermission(this, permission);
    if (grant != PackageManager.PERMISSION_GRANTED) {
         String[] permission_list = new String[1];
         permission_list[0] = permission;
         ActivityCompat.requestPermissions(this, permission_list, 1);
    }