Search code examples
javaandroidandroid-permissionsandroid-filefile-read

How to save password to file on android?


I got problem with writing a file to Android storage. I use a lot of examples, most annoying problem is

java.io.FileNotFoundException: /storage/emulated/0/Android/fileSD (Permission denied)

Yes, I'm adding uses-permission to my manifest

This is my Activity:

public class MainActivity extends Activity {
    // init variables
    EditText pass;
    Button login;
    String password;
//    final String DIR_SD = "/Android";
//    final String FILENAME_SD = "fileSD";
//    final String LOG_TAG = "myLogs";

//init onCreate method that provide our logic when activity start open
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

//        if(mFileName.()){
//
//        }

    pass = (EditText) findViewById(R.id.pass);
    login = (Button) findViewById(R.id.button);



    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            password = pass.getText().toString().trim();

                if (TextUtils.isEmpty(password)) {

                    pass.setError("password mustnot be empty");
                    pass.focusSearch(View.FOCUS_DOWN);

                } else if (password.length() >= 6 && password.length() <= 24) {

                    Toast.makeText(getApplicationContext(), "validation sucess", Toast.LENGTH_LONG).show();

                    Intent intent = new Intent(MainActivity.this, new_activity.class);
                    startActivity(intent);


                } else {
                    pass.setError("password length must be match:5=<password>=10");
                    pass.focusSearch(View.FOCUS_DOWN);
                }


          }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

if user open app at first time, he enter the password and it should write to file. If file exist we should read it and compare with pass that user will enter, if it equals start new activity, else display message that it was incorrect pass.

please help, i try do it for few days and idk what i should to do


Solution

  • Try to use SharedPreferences, you can save password like this

    SharedPreferences sharedPreferences = getSharedPreferences("PrefName", MODE_PRIVATE);
    sharedPreferences.edit().putString("password", "Your Password").apply();
    

    When you need it you can retrieve easily

    sharedPreferences.getString("password", "")