java.io.FileNotFoundException:/storage/emulated/0/Notes/fact50Result.txt:打开失败的ENOENT(没有这样的文件或目录)

时间:2017-11-17 11:46:46

标签: java android

我正在尝试在txt文件中导出阶乘计算的结果。 我发现这段代码看起来非常直接来自本文here. 但是我收到了标题上的错误。我做错了什么?

我已将其输入清单:

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

这是我的保存代码:

    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.

    }
}

3 个答案:

答案 0 :(得分:5)

正如评论中所述,您需要request permissions at runtime才能让您的应用写入外部存储空间。您可以使用checkSelfPermission()检查您的应用是否已获得许可。例如:



.controller('NotificacoesController', function($scope, $rootScope, laFactory) {
    var handler = $scope.$on('handleNotify', function () {
            alert('broadcast - on');
    });

要处理此权限请求,您还需要从.controller('NotificacoesController', function($scope, $rootScope, laFactory) { var handler = $scope.$on('handleNotify', function () { alert('broadcast - on'); }); 实施onRequestPermissionsResult(),例如:

}

阅读文档以获取完整信息。

答案 1 :(得分:2)

我认为您必须在运行时请求权限。请检查以下代码:

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

答案 2 :(得分:0)

对于具有相同问题的任何人,您可以按如下所示从合并清单中删除android:MaxSdkVersion属性。

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