FileNotFoundException:如何修复

时间:2019-07-04 12:00:59

标签: java android filenotfoundexception

我正在尝试读取文件并将其文本打印到TextView。但是,即使使用意图选择文件,它也会显示FileNotFoundException。我可以在logcat中看到路径,并且那里有文件,但是为什么会有错误。文件直接存储在内部存储器中。我该怎么办。

public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.delete:
                Intent deleteActivityIntent = new Intent(this, DeleteActivity.class);
                startActivity(deleteActivityIntent);
                return true;
            case R.id.scanFile:
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("text/plain");
                startActivityForResult(intent, GET_FILE);
                return true;
            case R.id.uploadImages:
                //TODO : upload Images to the server.
        }
        return super.onOptionsItemSelected(item);
    }

    void readFileData(Uri uri) {

        try {
            Log.e("loadkjdjhjf","akdjdshf");
            File file = new File(uri.getPath());
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            String line;
            StringBuilder stringBuilder = new StringBuilder();

            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line + '\n');
            }
            textView.setText(stringBuilder.toString());


        } catch (IOException e) {
            e.printStackTrace();
            Log.e("sssssssssssssssssshjf","akdasdddddddddddddddddddddddddddjdshf");

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == GET_FILE) {
            if (resultCode == RESULT_OK) {
                Uri uri = data.getData();
                readFileData(uri);
            }
        }

    }

Logcat

2019-07-04 17:09:03.530 14825-14825/com.example.freelancerwali E/loadkjdjhjf: akdjdshf
2019-07-04 17:09:03.531 14825-14825/com.example.freelancerwali W/System.err: java.io.FileNotFoundException: /document/primary:File.txt (No such file or directory)
2019-07-04 17:09:03.531 14825-14825/com.example.freelancerwali W/System.err:     at java.io.FileInputStream.open0(Native Method)
2019-07-04 17:09:03.532 14825-14825/com.example.freelancerwali W/System.err:     at java.io.FileInputStream.open(FileInputStream.java:231)
2019-07-04 17:09:03.532 14825-14825/com.example.freelancerwali W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:165)
2019-07-04 17:09:03.532 14825-14825/com.example.freelancerwali W/System.err:     at java.io.FileReader.<init>(FileReader.java:72)
2019-07-04 17:09:03.532 14825-14825/com.example.freelancerwali W/System.err:     at com.example.freelancerwali.MainActivity.readFileData(MainActivity.java:64)
2019-07-04 17:09:03.533 14825-14825/com.example.freelancerwali W/System.err:     at com.example.freelancerwali.MainActivity.onActivityResult(MainActivity.java:86)
2019-07-04 17:09:03.533 14825-14825/com.example.freelancerwali W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:7476)
2019-07-04 17:09:03.533 14825-14825/com.example.freelancerwali W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:4508)
2019-07-04 17:09:03.533 14825-14825/com.example.freelancerwali W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4557)
2019-07-04 17:09:03.533 14825-14825/com.example.freelancerwali W/System.err:     at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
2019-07-04 17:09:03.533 14825-14825/com.example.freelancerwali W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
2019-07-04 17:09:03.533 14825-14825/com.example.freelancerwali W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
2019-07-04 17:09:03.534 14825-14825/com.example.freelancerwali W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1920)
2019-07-04 17:09:03.534 14825-14825/com.example.freelancerwali W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
2019-07-04 17:09:03.534 14825-14825/com.example.freelancerwali W/System.err:     at android.os.Looper.loop(Looper.java:193)
2019-07-04 17:09:03.534 14825-14825/com.example.freelancerwali W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6912)
2019-07-04 17:09:03.534 14825-14825/com.example.freelancerwali W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2019-07-04 17:09:03.534 14825-14825/com.example.freelancerwali W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
2019-07-04 17:09:03.534 14825-14825/com.example.freelancerwali W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-07-04 17:09:03.534 14825-14825/com.example.freelancerwali E/sssssssssssssssssshjf: akdasdddddddddddddddddddddddddddjdshf

我在日志中打印了几行,以查看代码的运行方式。即使我认为不需要,我也添加了EXTERNAL_STORAGE_PERMISSION。但这并没有改变Logcat中的情况。

0 个答案:

没有答案