Android 输出为空。我试图通过要求用户输入文件名在手机模拟器的sd中保存一个文件

Android 输出为空。我试图通过要求用户输入文件名在手机模拟器的sd中保存一个文件,android,onclicklistener,fileoutputstream,Android,Onclicklistener,Fileoutputstream,我想在模拟器或手机上保存文件,方法是要求用户使用警报对话框输入文件名,然后系统将当前日期和时间添加到文件名中,但问题是系统通过null.txt保存文件名。如何解决此问题 SignSActivity.java 清单文件 您将在对话框的OK中获得文件名,因此只有在单击OK时才需要保存文件。试试这个 @Override public void onClick(View arg0) { // / for creating a dialog LayoutInfla

我想在模拟器或手机上保存文件,方法是要求用户使用警报对话框输入文件名,然后系统将当前日期和时间添加到文件名中,但问题是系统通过null.txt保存文件名。如何解决此问题

SignSActivity.java 清单文件
您将在
对话框的
OK
中获得文件名,因此只有在单击
OK
时才需要保存文件。试试这个

@Override
    public void onClick(View arg0) {

        // / for creating a dialog
        LayoutInflater li = LayoutInflater.from(context);
        View promptsView = li.inflate(R.layout.prompts, null);

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);

        // set prompts.xml to alertdialog builder
        alertDialogBuilder.setView(promptsView);

        final EditText userInput = (EditText) promptsView
                .findViewById(R.id.editTextDialogUserInput);

        // set dialog message
        alertDialogBuilder
                .setCancelable(false)
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // get user input and set it to result
                                // edit text
                                String userinputResult = userInput
                                        .getText().toString();

                                SimpleDateFormat formatter = new SimpleDateFormat(
                                        "yyyy/MM/dd\\HH:mm:ss");
                                Date now = new Date();
                                fileName = formatter.format(now) + "//"
                                        + userinputResult;

                                txtatePicker.setText(fileName);
                                // / for saving the file on the SD
                                writeFile(fileName);
                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                            }
                        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();



    }




    private void writeFile(String fileName){
         try {
                String sdPath = Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/" + fileName + ".txt";

                File myFile = new File(sdPath);
                myFile.createNewFile();

                Toast.makeText(getBaseContext(),
                        "the second step in saving file",
                        Toast.LENGTH_SHORT).show();

                FileOutputStream fOut = new FileOutputStream(myFile);
                OutputStreamWriter myOutWriter = new OutputStreamWriter(
                        fOut);

                // append or write
                myOutWriter.append(edit_txt_note.getText());
                myOutWriter.close();
                fOut.close();
                edit_txt_note.setText("");
                Toast.makeText(getBaseContext(),
                        "Done Writing SD" + fileName, Toast.LENGTH_SHORT)
                        .show();

            } catch (Exception e) {

                Toast.makeText(getBaseContext(), e.getMessage(),
                        Toast.LENGTH_SHORT).show();
            }
    }

创建另一种保存文件代码的方法,并将文件名作为参数从对话框“确定”按钮中传递,单击如下所示:

创建保存文件代码的方法

 public void saveFileinSDCard(String filename)
 {
    try {
        String sdPath = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/" + filename+ ".txt";

        File myFile = new File(sdPath);
        myFile.createNewFile();

        Toast.makeText(getBaseContext(),
                "the second step in saving file",
                Toast.LENGTH_SHORT).show();

        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(
                fOut);

        // append or write
        myOutWriter.append(edit_txt_note.getText());
        myOutWriter.close();
        fOut.close();
        edit_txt_note.setText("");
        Toast.makeText(getBaseContext(),
                "Done Writing SD" + fileName, Toast.LENGTH_SHORT)
                .show();

    } catch (Exception e) {

        Toast.makeText(getBaseContext(), e.getMessage(),
                Toast.LENGTH_SHORT).show();
    }
 }
在“确定”按钮中调用上述方法,单击如下所示:

     alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int id) {
                            // get user input and set it to result
                            // edit text
                            String userinputResult = userInput
                                    .getText().toString();

                            SimpleDateFormat formatter = new SimpleDateFormat(
                                    "yyyy/MM/dd\\HH:mm:ss");
                            Date now = new Date();
                            fileName = formatter.format(now) + "//"
                                    + userinputResult;

                             saveFileinSDCard(fileName); //Call the method here.

                            txtatePicker.setText(fileName);
                        }
                    })

您是否尝试过调试代码并打印文件名?请检查文件名是否返回null。我尝试过您的解决方案,但在单击“保存”时,在输入文件名之前,系统仍将文件名保存为null。txtwrite file save code in
OK
按钮单击。@Md Abdul Gafur在我想尝试您的答案时输入文件返回字符串现在它给了我一个新的错误:open failed:Enoint(没有这样的文件或目录)并且系统没有保存文件你给了
WRITE\u EXTERNAL\u STORAGE
权限了吗,你有sd卡吗?检查这个,是的,我有这个权限,我有sd卡,但仍然是相同的错误对不起,不知道,我尝试了你的答案,现在它给了我一个新的错误:open失败:enoint(没有这样的文件或目录),系统没有保存file@user3006788是否在清单文件中添加了对外部存储的写入和读取权限???@user3006788在文件名的
fileName=formatter.format(现在)+“/”行中删除一个
/
斜杠+用户输入结果
并将其更改为
fileName=formatter.format(现在)+“/”+userinputResult
然后选中@GrlsHu i add log Cat,它将错误显示为Null异常,错误在哪里???@user3006788是否创建了骨架应用程序类??
@Override
    public void onClick(View arg0) {

        // / for creating a dialog
        LayoutInflater li = LayoutInflater.from(context);
        View promptsView = li.inflate(R.layout.prompts, null);

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);

        // set prompts.xml to alertdialog builder
        alertDialogBuilder.setView(promptsView);

        final EditText userInput = (EditText) promptsView
                .findViewById(R.id.editTextDialogUserInput);

        // set dialog message
        alertDialogBuilder
                .setCancelable(false)
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // get user input and set it to result
                                // edit text
                                String userinputResult = userInput
                                        .getText().toString();

                                SimpleDateFormat formatter = new SimpleDateFormat(
                                        "yyyy/MM/dd\\HH:mm:ss");
                                Date now = new Date();
                                fileName = formatter.format(now) + "//"
                                        + userinputResult;

                                txtatePicker.setText(fileName);
                                // / for saving the file on the SD
                                writeFile(fileName);
                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                            }
                        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();



    }




    private void writeFile(String fileName){
         try {
                String sdPath = Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/" + fileName + ".txt";

                File myFile = new File(sdPath);
                myFile.createNewFile();

                Toast.makeText(getBaseContext(),
                        "the second step in saving file",
                        Toast.LENGTH_SHORT).show();

                FileOutputStream fOut = new FileOutputStream(myFile);
                OutputStreamWriter myOutWriter = new OutputStreamWriter(
                        fOut);

                // append or write
                myOutWriter.append(edit_txt_note.getText());
                myOutWriter.close();
                fOut.close();
                edit_txt_note.setText("");
                Toast.makeText(getBaseContext(),
                        "Done Writing SD" + fileName, Toast.LENGTH_SHORT)
                        .show();

            } catch (Exception e) {

                Toast.makeText(getBaseContext(), e.getMessage(),
                        Toast.LENGTH_SHORT).show();
            }
    }
 public void saveFileinSDCard(String filename)
 {
    try {
        String sdPath = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/" + filename+ ".txt";

        File myFile = new File(sdPath);
        myFile.createNewFile();

        Toast.makeText(getBaseContext(),
                "the second step in saving file",
                Toast.LENGTH_SHORT).show();

        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(
                fOut);

        // append or write
        myOutWriter.append(edit_txt_note.getText());
        myOutWriter.close();
        fOut.close();
        edit_txt_note.setText("");
        Toast.makeText(getBaseContext(),
                "Done Writing SD" + fileName, Toast.LENGTH_SHORT)
                .show();

    } catch (Exception e) {

        Toast.makeText(getBaseContext(), e.getMessage(),
                Toast.LENGTH_SHORT).show();
    }
 }
     alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int id) {
                            // get user input and set it to result
                            // edit text
                            String userinputResult = userInput
                                    .getText().toString();

                            SimpleDateFormat formatter = new SimpleDateFormat(
                                    "yyyy/MM/dd\\HH:mm:ss");
                            Date now = new Date();
                            fileName = formatter.format(now) + "//"
                                    + userinputResult;

                             saveFileinSDCard(fileName); //Call the method here.

                            txtatePicker.setText(fileName);
                        }
                    })