There was a problem saving the text in EditText to a file

Viewed 30

This code receives information from an acquaintance you want to register in editText, and then clicks finButton to save the information you receive as a file called friendlist.txt. However, the Toast message is outputted from the try-catch statement that is currently performed when finButton is pressed. Also, the checkpermission does not work, which is wrapped in a try~catch statement, but does not have output on the logcat.

And manifest.

uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

is written.

Please let me know the solution. And this content is written with a translator, so the sentence can be strange.

when you press finButton, the logcat is shown below.

The code corresponding to the 116th line is this.

FileOutputStream outstream = openFileOutput("friendList.txt", Activity.MODE_WORLD_WRITEABLE);

logcat

public class EnteringInformationOfFriendActivity extends AppCompatActivity {
    Button finButton;
    EditText phoneNumberEditText, nameEditText, emailEditText, groupEditText, memoEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_entering_information_of_friend);

        phoneNumberEditText = (EditText) findViewById(R.id.phoneNumberEditText);

        nameEditText = (EditText) findViewById(R.id.nameEditText);
        emailEditText = (EditText) findViewById(R.id.emailEditText);
        groupEditText = (EditText) findViewById(R.id.groupEditText);
        memoEditText = (EditText) findViewById(R.id.memoEditText);

        try{
            checkPermission();
        }
        catch (Exception e){
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            String exceptionAsStrting = sw.toString();
            Log.e("CSP", exceptionAsStrting);

            e.printStackTrace();
        }

        finButton = (Button) findViewById(R.id.finButton);
        finButton.setOnClickListener(v -> {
            try{
                String phoneTxt = phoneNumberEditText.getText().toString().trim();
                String nameTxt = nameEditText.getText().toString().trim();
                String emailTxt = emailEditText.getText().toString().trim();
                String groupTxt = groupEditText.getText().toString().trim();
                String memoTxt = memoEditText.getText().toString().trim();

                String friendInfoTxt = phoneTxt + "-" + nameTxt + "-" + emailTxt + "-" + groupTxt + "-" + memoTxt;

                FileOutputStream outstream = openFileOutput("friendList.txt", Activity.MODE_WORLD_WRITEABLE);

                outstream.write(friendInfoTxt.getBytes());

                outstream.close();

                Intent intent = new Intent(v.getContext(), FriendListActivity.class);
                startActivity(intent);
            } catch (Exception e){
                StringWriter sw = new StringWriter();
                e.printStackTrace(new PrintWriter(sw));
                String exceptionAsStrting = sw.toString();
                Log.e("Filesave", exceptionAsStrting);

                e.printStackTrace();
                Toast.makeText(this.getApplicationContext(), "Save failed.", Toast.LENGTH_SHORT).show();
            }
        });
    }
    public void checkPermission(){
        if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    }
}
1 Answers

Try with Context.MODE_APPEND or Context.MODE_PRIVATE instead of Activity.MODE_WORLD_WRITEABLE

Related