Sunday, January 27, 2013

[android-developers] Re: Input/Output stream

If I understand you correctly you just want to pass on strings from one Activity to the other. You should use the Intent object that calls the other Activity for storing that string as an "extra":

intent.putExtra(EXTRA_KEY, "The string you want to pass on");
startActivitiy(intent);

Where "EXTRA_KEY" itself is a string that serves as a key for retrieving the string data again from the other activity.

In the other Activity you can read the String with:

String myString = getIntent().getStringExtra(EXTRA_KEY);


On Sunday, January 27, 2013 11:54:12 AM UTC-6, tgundhus wrote:
Hello.

I am using the example from the android development sites on outputstream to save a string to the internal storage, and I'm trying to get it with the use of inputstream.. But can't get it to work =/ What I want is to store a string and easily be able to get it from other activities, thats why I think this solution was the best. If you got any other solutions, please tell.

Anyway I am using this script, the string MyData is defined further up in the document as well as the filename. 

String filename = "testfile" //also tried "testfile.txt  
....
if (selectedItem.equals("Get")) {
       
try {
         
FileInputStream fis = new FileInputStream(filename);
         
DataInputStream in = new DataInputStream(fis);
         
BufferedReader br = new BufferedReader(
           
new InputStreamReader(in));
         
String strLine;
         
while ((strLine = br.readLine()) != null) {
          myData
= myData + strLine;
         
}
         
in.close();
       
} catch (IOException e) {
         e
.printStackTrace();
       
}
   
}
   
if (selectedItem.equals("Post")) {
       
String test = "5556";
       
FileOutputStream outputStream;
       
try {
         outputStream
= openFileOutput(filename,
           
Context.MODE_PRIVATE);
         outputStream
.write(test.getBytes());
         outputStream
.close();
       
} catch (Exception e) {
         e
.printStackTrace();
       
}
   
}

--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
 
 

No comments:

Post a Comment