在下载目录中写入问题 [英] Issue writing in the Download directory

查看:59
本文介绍了在下载目录中写入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to write this xlsx file in the Download directory of a Samsung Galaxy Tab A 2019 (Android 9.0). If I try to do this on my emulator (Google Pixel C with android 9.0) it works without any problems and I can see the file. If I give the app to my client it gives an error, catches by up by this function:

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.这是行不通的方法:

Unfortunately I cannot see the stack trace since I cannot connect the client's tablet to my PC. This is the method which doesn't works:

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();
}

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

I also wrote this method which saves in the same directory and it works, so I don't think it's a permission problem:

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();
                        }
                    }
                }
            }
        }
    }
}

推荐答案

在某些设备上和更新的Android版本中, Environment.getExternalStorageDirectory()不再返回有效路径.尝试改用 Context.getExternalFilesDir(null),它应返回以下路径:/storage/emulated/0/Android/data/your.package.name/.尝试一下,看看这是否是问题所在.这是 doc .

On some devices and in newer Android versions Environment.getExternalStorageDirectory() no longer returns a valid path. Try using Context.getExternalFilesDir(null) instead, it should return this path: /storage/emulated/0/Android/data/your.package.name/. Try it out to see if thats the issue. Here's the doc.

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

I recommend you to emulate some similar Samsung devices and see if you can replicate the error to have a look at the logcat output

这篇关于在下载目录中写入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆