java - Android更新后opencsv不读取CSV

标签 java android opencsv

在昨晚我的手机更新之前,我的代码运行良好。
我正在使用 opencsv 将手机存储中的 CSV 文件读取到我的应用程序中的数组中。
这是代码...

    public static String[] getFileContents(File file) {
    String[] contentsArray = new String[500];
    if(file.exists()) {
        try {
            CSVReader reader = new CSVReader(new FileReader(file));
            String[] nextLine;
            while ((nextLine = reader.readNext()) != null) {
                System.arraycopy(nextLine, 0, contentsArray, 0, nextLine.length);
            }
        } catch (Exception e) {
            System.out.println("Failed file: " + file);
            System.out.println("You are here and sad but at least the file exists");
            e.printStackTrace();
        }
    } else {
        System.out.println("File does not exist");
    }

    return contentsArray;
}
这是我得到的错误......
I/System.out: Failed file: /storage/emulated/0/Documents/text/36-12644-Test cert-2021.csv
I/System.out: You are here and sad but at least the file exists
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Documents/text/36-12644-Test cert-2021.csv: open failed: EACCES (Permission denied)
我什么都没改变。谁能指出我正确的方向来解决这个问题?
新更新(Android 11)中是否发生了一些变化,现在阻止 openCSV 读取文件的权限?
编辑:
这是从 fileExplorer 中引入所选文件的代码...
                    Uri fileUri = data.getData();
                String filePath;
                try {
                    filePath = Utils.getRealPathFromURI(this.getContext(), fileUri);
                    selectedFile = new File(filePath);
                    contentsFromFile = Utils.getFileContents(selectedFile.getAbsoluteFile());
这是获取路径的代码...
    public static String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Images.Media.DATA};
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } catch (Exception e) {
        Log.e(TAG, "getRealPathFromURI Exception : " + e.toString());
        return;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

最佳答案

感谢 CommonsWare,我现在可以让代码再次运行。
他们的解决方案是

delete getRealPathFromURI(). Use ContentResolver and openInputStream() to get an InputStream on the content. Wrap that InputStream in an InputStreamReader. Pass that InputStreamReader to CSVReader.


具体来说...
InputStream input = this.getContext().getContentResolver().openInputStream(fileUri);
CSVReader reader = new CSVReader(new InputStreamReader(input));

关于java - Android更新后opencsv不读取CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65957624/

相关文章:

android - 以编程方式注册的广播接收器将直播多长时间?

java - 通过 Rest 下载 CSV 文件

java - Opencsv将所有数据存储在单列中

java - 防止后台进程出现在 Dock 上

java - 如何为 try & catch 实现指数延迟/ sleep

android - 同步ViewPager中的所有ListView

java - 可扩展 ListView subview 问题

java - 使用 open csv 时从 csv 中删除双引号

java - 如何知道 HttpURLConnection 是否确实在底层套接字上发送了请求?

Java JEditorPane 用超链接替换选中的文本