No file are created when I use createNewFile()?

247 views Asked by At

I follow the video https://www.youtube.com/watch?v=MSgJJzhSI-A and try to create a Sheet, but no file are created, I notice that there is a Warning of createNewFile(): Result of File.createNewFIle() is ignored. How can I fix this?

private File filepath = new File(Environment.getExternalStorageDirectory()+"/AndroidExcelDemo/Demo.xls");

public void CreatSheet() {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        HSSFSheet hssfSheet = hssfWorkbook.createSheet();
        HSSFRow hssfRow = hssfSheet.createRow(0);
        HSSFCell hssfCell = hssfRow.createCell(0);
        hssfCell.setCellValue("Test1");
        try {
            if (!filepath.exists()) {
                filepath.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(filepath);
            hssfWorkbook.write(fileOutputStream);

            if (fileOutputStream != null) {
                fileOutputStream.flush();
                fileOutputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch(item.getItemId()){
                case R.id.menu_export:
                CreatSheet();
                return true;
}

I have

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" \>

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

in Manifest

0

There are 0 answers