我使用apache.i创建Excel文件时收到类似Permission denied
的错误,但我在清单文件中添加了权限。
以我为pdf创建的类似方式,它正在发挥作用。
我参考了这个教程: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"/>
我的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");
}
运行时错误:
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)
请在这种情况下提供帮助。谢谢。
答案 0 :(得分:0)
我认为你需要问跑步时间 从Android 6.0(API级别23)开始,用户在应用运行时向应用授予权限,而不是在安装应用时授予权限。
<强>码强>
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);
}