首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel文件未使用Apache存储内部内存

Excel文件未使用Apache存储内部内存
EN

Stack Overflow用户
提问于 2017-09-20 14:35:36
回答 1查看 60关注 0票数 0

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

用我为pdf创建的类似方式,它是有效的。

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

代码语言:javascript
复制
 <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

我的java代码如下:

代码语言:javascript
复制
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");


    }

运行时错误:

代码语言:javascript
复制
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)

请提前在此case..Thanks中提供帮助。

EN

回答 1

Stack Overflow用户

发布于 2017-09-20 14:37:21

我认为你需要询问运行时权限,因为从Android6.0( level 23)开始,用户在应用运行时授予应用权限,而不是在安装应用时授予权限。

代码

代码语言:javascript
复制
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);
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46314860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档