我正在尝试将此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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。