首页 > 解决方案 > 逐行读取文件

问题描述

我正在创建一个 Android 应用程序来跟踪财务费用,我希望允许用户将财务交易批量导入我的应用程序。交易将以 .csv/.txt 文件的形式出现。

但是,我遇到了一个神秘的异常:

java.lang.AbstractMethodError: abstract method "android.content.IContentProvider  android.content.ContentResolver.acquireUnstableProvider(android.content.Context, java.lang.String)"
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1780)
   at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1394)
   at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1247)
   at android.content.ContentResolver.openInputStream(ContentResolver.java:967)
 ....

工作流程如下:用户选择要导入的文本文件,应用程序导入内容。

启动文件选择器:

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("*/*");
//intent.addCategory("CATEGORY_OPENABLE");

startActivityForResult(intent, REEQUEST_CODE_IMPORT);

捕捉结果:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == REEQUEST_CODE_IMPORT) {
            // Make sure the request was successful
            Uri path = data.getData();

            if (resultCode == RESULT_OK && path != null) {
                InputStream inputStream = null;
                try {
                    ContentResolver contentResolver = new ContentResolver(getContext()) {};

                    // Error happens in the next line
                    inputStream = contentResolver.openInputStream(path);

                    // Get the object of DataInputStream
                    DataInputStream in = new DataInputStream(inputStream);
                    BufferedReader br = new BufferedReader(new InputStreamReader(in));
                    String line = "";

                    while((line = br.readLine()) != null) {
                        // Do something meaningful... 
                    }

                } catch () {
                    // Catch the exceptions ( I have removed some boiler plate code here...)
                } finally {
                    // Close the path ( I have removed some boiler plate code here...) 
                        inputStream.close();
                    }
                }
            }
        }

标签: javaandroidfileandroid-intent

解决方案


Android 文档帮助我解决了这个问题:Access document and other files from shared storage - Input Stream

以下代码适应工作:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == REEQUEST_CODE_IMPORT) {
            // Make sure the request was successful
            Uri path = data.getData();

            if (resultCode == RESULT_OK && path != null) {
                    StringBuilder stringBuilder = new StringBuilder();
                    try (InputStream inputStream = getActivity().getContentResolver().openInputStream(path);
                         BufferedReader reader = new BufferedReader(
                                 new InputStreamReader(Objects.requireNonNull(inputStream)))) {
                        String line;
                        while ((line = reader.readLine()) != null) {
                            Log.d(TAG, line);
                            stringBuilder.append(line);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    String content = stringBuilder.toString();
                    // Do something with it
            }
        }
    }

推荐阅读