开发者社区> 问答> 正文

在下载目录中写入问题

我正在尝试将此xlsx文件写入Samsung Galaxy Tab A 2019(Android 9.0)的下载目录中。如果我尝试在模拟器(带有Android 9.0的Google Pixel C)上执行此操作,则不会出现任何问题,并且可以看到该文件。如果我将应用程序提供给我的客户端,则会出现错误,此功能会引起追赶:

try {
     importIntoExcel();
     DynamicToast.makeSuccess(UserList.this, "Saved!", 2000).show();
     b1.setEnabled(true);
} catch (IOException e) {
     DynamicToast.makeError(UserList.this, "Error!", 2000).show();
     e.printStackTrace();
}

不幸的是,由于无法将客户端的平板电脑连接到PC,因此无法看到堆栈跟踪。这是行不通的方法:

private void importIntoExcel() throws IOException {
    String[] columns = {"Numero Test", "Codice ID", "Genere", "Data di nascita", "Protocollo", "Data del test", " ", "Cornice", "Nome cornice", "Fluidità", "Flessibilità",
            "Originalita'", "Elaborazione'", "Titolo", "Tempo Reazione", "Tempo Completamento", "Numero cancellature", "Numero Undo"};


    Workbook workbook = new XSSFWorkbook();
    Sheet sheet = workbook.createSheet("RiepilogoTest");

    Font headerFont = workbook.createFont();
    headerFont.setBold(true);
    headerFont.setFontHeightInPoints((short) 14);
    headerFont.setColor(IndexedColors.RED.getIndex());

    CellStyle headerCellStyle = workbook.createCellStyle();
    headerCellStyle.setFont(headerFont);
    headerCellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION);

    // Create a Row
    Row headerRow = sheet.createRow(0);

    for (int i = 0; i < columns.length; i++) {
        Cell cell = headerRow.createCell(i);
        cell.setCellValue(columns[i]);
        cell.setCellStyle(headerCellStyle);

    }

    // Create Other rows and cells with contacts data
    int rowNum = 1;

    //Inserting the data
    File dir = new File("/data/user/0/com.example.williamstest/");

    for (File file : dir.listFiles()) {
        if (file.getName().startsWith("app_draw")) {
            String typeTest = file.getName().replaceAll("[^\\d.]", "");
            if (new File(file.getAbsolutePath() + "/infotest.txt").exists()) {
                FileReader f = new FileReader(file.getAbsolutePath() + "/infotest.txt");
                LineNumberReader reader = new LineNumberReader(f);
                String line;
                String protocollo = "";
                line = reader.readLine();
                Row row = null;
                if (line.equals(userLogged)) {
                    row = sheet.createRow(rowNum++);
                    row.createCell(0).setCellValue("Test: " + typeTest);
                    line = reader.readLine();
                    row.createCell(2).setCellValue(line);
                    line = reader.readLine();
                    if (line.equals("0")) row.createCell(2).setCellValue("/");
                    row.createCell(3).setCellValue(line);
                    line = reader.readLine();
                    protocollo = line;
                    row.createCell(4).setCellValue(line);
                    line = reader.readLine();
                    row.createCell(5).setCellValue(line);
                    line = reader.readLine();
                    row.createCell(1).setCellValue(line);
                }
                for (int i=0; i<12; i++) {
                    String content = "";
                    reader = new LineNumberReader(new FileReader(file.getAbsolutePath() + "/" + protocollo + (i + 1) + "_score.txt"));
                    while ((line = reader.readLine()) != null) {
                        content+=line+"\n";
                    }

                    String[] values = content.split("\n");
                    row.createCell(6).setCellValue(" "); //Vuota
                    row.createCell(7).setCellValue(i+1); //Cornice
                    row.createCell(8).setCellValue(values[4]); //Nome cornice
                    row.createCell(9).setCellValue(values[0]); //Fluidita
                    row.createCell(10).setCellValue(values[1]); //Flessibilita
                    row.createCell(11).setCellValue(values[2]); //Originalita'
                    row.createCell(12).setCellValue(values[3]); //Elaborazione
                    row.createCell(13).setCellValue(values[9]); //Titolo
                    row.createCell(14).setCellValue(values[5]); //Tempo reazione
                    row.createCell(15).setCellValue(values[6]); //Tempo Completamento
                    row.createCell(16).setCellValue(values[7]); //Numero cancellature
                    row.createCell(17).setCellValue(values[8]); //Numero undo

                    row = sheet.createRow(rowNum++);
                    row.createCell(0).setCellValue(" ");
                    row.createCell(1).setCellValue(" ");
                    row.createCell(2).setCellValue(" ");
                    row.createCell(3).setCellValue(" ");
                    row.createCell(4).setCellValue(" ");
                    row.createCell(5).setCellValue(" ");
                }
                f.close();
            }
        }
    }

    sheet.setDefaultColumnWidth(23);

    // Write the output to a file
    if (new File(Environment.getExternalStorageDirectory(), "Download/risultatiTest.xlsx").exists())
        new File(Environment.getExternalStorageDirectory(), "Download/risultatiTest.xlsx").delete();
    FileOutputStream fileOut = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "Download/risultatiTest.xlsx"));
    workbook.write(fileOut);
    fileOut.close();
}

我还编写了此方法,该方法保存在同一目录中并且可以工作,因此我认为这不是权限问题:

private void generateImages() throws IOException {
    File dir = new File("/data/user/0/com.example.williamstest/");
    File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "/Download/ImmaginiTest");
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs())
            Log.d("App", "failed to create directory");
    } else {
            if (mediaStorageDir.isDirectory()) {
                for (File child : mediaStorageDir.listFiles())
                    deleteRecursive(child);
            }
            mediaStorageDir.delete();
            mediaStorageDir.mkdirs();
        }
    for (File file : dir.listFiles()) {
        if (file.getName().startsWith("app_draw") && Character.isDigit(file.getName().charAt(file.getName().length() - 1))) {
            File makingDir = new File(Environment.getExternalStorageDirectory(), "/Download/ImmaginiTest/Test"+file.getName().substring(file.getName().length() - 1));
            makingDir.mkdirs();
            for (File fileS : file.listFiles()) {
                if (fileS.getName().endsWith(".png")) {
                    Bitmap b = BitmapFactory.decodeStream(new FileInputStream(fileS));
                    File mypath=new File(makingDir, fileS.getName());
                    FileOutputStream fos = null;
                    try {
                        fos = new FileOutputStream(mypath);
                        b.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

问题来源:Stack Overflow

展开
收起
montos 2020-03-27 22:56:39 528 0
1 条回答
写回答
取消 提交回答
  • 在某些设备上和较新的Android版本中,Environment.getExternalStorageDirectory()不再返回有效路径。尝试Context.getExternalFilesDir(null)改用,它应该返回以下路径:/storage/emulated/0/Android/data/your.package.name/。尝试一下,看是否是问题所在。这是文档

    我建议您模拟一些类似的Samsung设备,看看是否可以复制错误以查看logcat输出

    回答来源:Stack Overflow

    2020-03-27 22:57:18
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
附件下载测试 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载