使用按钮创建文件

时间:2019-02-27 14:16:09

标签: java android

我正在尝试使用用户输入的信息来创建文本文档,但是什么也没有创建...这是我用于按钮单击的代码:

runnerTestResultBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (capitanPasswordValidate()& permissionGranted) {

                if (capitanPasswordET.getText().toString().equals(capitanPassword)) {

                    createFile();

                } else {

                    capitanPasswordError.setText("Wrong Password!");
                    capitanPasswordError.requestFocus();


                }

            }
        }
    });

此外,这是我用来创建文件的代码:

private void createFile() {

    String familiaName = familiaNamesSpinner.getSelectedItem().toString();
    String FILE_NAME = familiaName + "_Runner_Test_Results.txt";
    String cafeteroTestResultString = runnerTestResult1.getText().toString();

    FileOutputStream fos = null;
    File file = new File(FILE_NAME);

    try {
            capitanPasswordError.setText("");
            fos = new FileOutputStream(file);
            fos.write(cafeteroTestResultString.getBytes());
            fos.close();
            Toast.makeText(this, "worked", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(this, "did not work", Toast.LENGTH_SHORT).show();
    } finally {
        if (fos != null) try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

请注意,我正在这样做,以便用户从微调器视图中选择一个选项,以便跟踪哪个文件属于哪个人。它在familiaName下,将成为创建的文件名的一部分。任何反馈都将是惊人的,谢谢!!

1 个答案:

答案 0 :(得分:1)

在读取或写入文件之前,您必须检查权限。请在清单中添加权限请求,并在运行时请求READ,WRITE操作的权限,

这里我有一个简单的解决方案,-(多次权限检查)

String[] permissions = new String[]{
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE}; // Here i used multiple permission check

然后在Oncreate中调用它

 if (checkPermissions()) {
            //  permissions  granted.
            getCallDetails();
        }

最后,复制下面的代码

private boolean checkPermissions() {
        int result;
        List<String> listPermissionsNeeded = new ArrayList<>();
        for (String p : permissions) {
            result = ContextCompat.checkSelfPermission(getApplicationContext(), p);
            if (result != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(p);
            }
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), MULTIPLE_PERMISSIONS);
            return false;
        }
        return true;
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MULTIPLE_PERMISSIONS: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permissions granted.
                    getCallDetails(); // Now you call here what ever you want :)
                } else {
                    String perStr = "";
                    for (String per : permissions) {
                        perStr += "\n" + per;
                    }
                    // permissions list of don't granted permission
                }
                return;
            }
        }
    }