简体   繁体   English

java.io.FileNotFoundException: /storage/emulated/0/Notes/fact50Result.txt: open failed ENOENT (No such file or directory)

[英]java.io.FileNotFoundException: /storage/emulated/0/Notes/fact50Result.txt: open failed ENOENT (No such file or directory)

I am trying to export the results of a factorial calculation in a txt file.我正在尝试将阶乘计算的结果导出到 txt 文件中。 I found this code that looked quite straight forward from this article here.我发现这段代码在这篇文章中看起来很直接 However I'm getting the error on the title.但是我收到了标题错误。 What am I doing wrong?我究竟做错了什么?

I have typed this into the manifest:我已将其输入到清单中:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

And this is my code related to saving:这是我与保存相关的代码:

    public void save(View v) {
     if(numOnRAM!=-1) {
         EditText text = (EditText) findViewById(R.id.resultTextBox);
         String data = text.getText().toString();
         generateNoteOnSD(this,"fact"+numOnRAM+"Results.txt", data);
     }
}

public void generateNoteOnSD(Context context, String sFileName, String sBody) {
    try {
        File root = new File(Environment.getExternalStorageDirectory(), "Notes");
        if (!root.exists()) {
            root.mkdirs();
        }
        File gpxfile = new File(root, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();
        Toast.makeText(context, "File "+sFileName+" saving success!", Toast.LENGTH_LONG).show();

    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();//After adding this 
//toast message did I realise the error thar occurs.

    }
}

As discussed in the comments, you need to request permissions at runtime to allow your app to write to external storage.正如评论中所讨论的,您需要在运行时请求权限以允许您的应用程序写入外部存储。 You can check if your app has the permission already by using checkSelfPermission() .您可以使用checkSelfPermission()来检查您的应用是否已经拥有该权限。 For example:例如:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
    // Do the file write
} else {
    // Request permission from the user
    ActivityCompat.requestPermissions(this, 
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
    ...
}

In order to handle this permission request, you'll also need to implement onRequestPermissionsResult() from OnRequestPermissionsResultCallback , for example:为了处理此权限请求,您还需要从OnRequestPermissionsResultCallback实现onRequestPermissionsResult() ,例如:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case 0:
            // Re-attempt file write
    }
}

Read the documentation for full information.阅读文档以获取完整信息。

I think you have to request permission on runtime.我认为您必须在运行时请求许可。 Please check the code below:请检查以下代码:

 final private int REQUEST_CODE_ASK_PERMISSIONS = 123;

      private void requestPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat
                    .requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_ASK_PERMISSIONS);
        }
    }

        @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_PERMISSIONS:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission Granted
                    Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT)
                            .show();
                } else {
                    // Permission Denied
                    Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT)
                            .show();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

For anyone having the same issue, you can remove the android:MaxSdkVersion attribute from the merged manifest as follows.对于遇到相同问题的任何人,您可以从合并的清单中删除 android:MaxSdkVersion 属性,如下所示。

<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    tools:remove="android:maxSdkVersion" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android - java.io.FileNotFoundException: /storage/emulated/0/Notes/File.txt: open failed: ENOENT (No such file or directory) - Android - java.io.FileNotFoundException: /storage/emulated/0/Notes/File.txt: open failed: ENOENT (No such file or directory) Android/Java:I/System.out:错误:java.io.FileNotFoundException:/storage/emulated/0/hermaps/...:打开失败:ENOENT(没有这样的文件或目录) - Android/Java: I/System.out: Error :java.io.FileNotFoundException: /storage/emulated/0/heremaps/...: open failed: ENOENT (No such file or directory) java.io.FileNotFoundException:/storage/emulated/0/New file.txt:打开失败:EACCES(权限被拒绝) - java.io.FileNotFoundException: /storage/emulated/0/New file.txt: open failed: EACCES (Permission denied) Android - java.io.FileNotFoundException打开失败:ENOENT(没有这样的文件或目录) - Android - java.io.FileNotFoundException open failed: ENOENT (No such file or directory) java.io.FileNotFoundException:打开失败:ENOENT(没有这样的文件或目录) - java.io.FileNotFoundException: open failed: ENOENT (No such file or directory) FileNotFoundException:/storage/emulated/0/JsonParseTutorialCache:打开失败:ENOENT(没有这样的文件或目录) - FileNotFoundException: /storage/emulated/0/JsonParseTutorialCache: open failed: ENOENT (No such file or directory) java.io.FileNotFoundException,打开失败:ENOENT - java.io.FileNotFoundException, open failed: ENOENT java.io.FileNotFoundException:/ drawable:/ 2130837597:打开失败:ENOENT(没有此类文件或目录)在通过电子邮件附加文件时 - java.io.FileNotFoundException: /drawable:/2130837597: open failed: ENOENT (No such file or directory) On Attaching File With Email 本地html文件始终导致java.io.FileNotFoundException:打开失败:ENOENT(无此类文件或目录) - Local html file always results to java.io.FileNotFoundException: open failed: ENOENT (No such file or directory) 读取文件时出错:java.io.FileNotFoundException:打开失败:ENOENT(无此类文件或目录) - Error reading file: java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM