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)
この場合は助けてください..よろしくお願いします。