Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7521423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:13:58+00:00 2026-05-30T02:13:58+00:00

I’m trying to permanently store three strings as user preferences for my Android application.

  • 0

I’m trying to permanently store three strings as user preferences for my Android application. These three strings are a url, username, and password. I don’t really understand SharedPreferences, so I tried to use internal file storage. I’m not able to retrieve the three strings from the file, and I get a runtime error. I know I probably coded something wrong, but I’m just not proficient enough in Android to understand data storage. Could somebody help me out?

Preferences activity:

package com.amritayalur.mypowerschool;



import java.io.File;
import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
 import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class MyPowerSchoolActivity extends Activity {
Button buttonSubmit;
TextView textViewTitle;
TextView textViewDesc;
EditText editTextURL, editTextUser, editTextPass;
FileOutputStream fos;
String url = "";
String FILENAME = "InternalStrings";
 String str;
 String username;
 String password;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    buttonSubmit = (Button) findViewById(R.id.buttonSubmit);
    textViewTitle = (TextView) findViewById(R.id.textViewTitle);
    textViewDesc = (TextView) findViewById(R.id.textViewDesc);

    editTextURL = (EditText) findViewById(R.id.editTextURL);
    editTextUser = (EditText) findViewById(R.id.editTextUser);
    editTextPass = (EditText) findViewById(R.id.editTextPass);
    //Start TextView
    textViewTitle.setText("MyPowerSchool");
    try {
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
        fos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    //button listener
    buttonSubmit.setOnClickListener(new View.OnClickListener() {

         @Override
            public void onClick(View v) 
            {
                if (  ( !editTextURL.getText().toString().equals("")) && (
!editTextUser.getText().toString().equals("")) && (
!editTextPass.getText().toString().equals("") ) ) 
                {
                     url = editTextURL.getText().toString();
                     username = editTextUser.getText().toString();
                     password = editTextPass.getText().toString();
                    //Saving data via File
                    /* File f = new File(FILENAME);
                     try {
                        fos = new FileOutputStream(f);
                        fos.close();
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                        */
                     try {
                     fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);

                        fos.write(url.getBytes());
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                     try {
                        fos.write(username.getBytes());
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                     try {
                        fos.write(password.getBytes());
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                   try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



                    // TODO Auto-generated method stub
                    Intent i = new Intent( MyPowerSchoolActivity.this,
creds.class);    
                    //i.putExtra("pschoolurl", editTextURL.getText().toString());
                    //i.putExtra("pschooluser", editTextUser.getText().toString());
                    //i.putExtra("pschoolpass", editTextPass.getText().toString());
                   // get the text here
                   final int result = 1;
                   startActivityForResult(i, result); 
                }
            };




});}}

Activity in which I am trying to retrieve credentials:

package com.amritayalur.mypowerschool;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;



public class creds extends Activity {

String url;
String username;
String password;
TextView TextViewTest;

    String FILENAME = "InternalStrings";
;
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);




    Intent intent = getIntent();
    //String url = intent.getExtras().getString("pschoolurl");
    //String username = intent.getExtras().getString("pschooluser");
    //String password = intent.getExtras().getString("pschoolpass");
    String collected = null;
    FileInputStream fis = null;
    try {
        fis = openFileInput(FILENAME);
        byte[] dataArray = new byte[fis.available()];
        while (fis.read(dataArray) != -1 ){
            collected = new String(dataArray);

        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            fis.close();
            TextViewTest.setText(collected);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }






}
}

The commented text is a result of me trying to mess around with different aspects of the code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:13:58+00:00Added an answer on May 30, 2026 at 2:13 am

    SharedPreferences is not too difficult.

    To add something to the preferences:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MyActivity.this); //Get the preferences
    Editor edit = prefs.edit(); //Needed to edit the preferences
    
    edit.putString("name", "myname");  //add a String
    edit.putBoolean("rememberCredentials", true); //add a boolean
    
    edit.commit();  // save the edits.
    

    To read something:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MyActivity.this); //Get the preferences
    String name = prefs.getString("name", "defaultName"); //get a String
    boolean rememberCredentials = prefs.getBoolean("rememberCredentials", true); //get a boolean. 
    //When the key "rememberCredentials" is not present, true is returned.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.