How to save data till application un-install in android?
In Android the SharedPreference and SQLite databases are cleared once the user clear's da开发者_C百科ta from Manage Applications -> Application Name -> clears Data. What if there is some information that should be persisted till the application is installed. I want to store the info till the application is installed in the device.
Is there a way to actually to do this?
Isn't there any way of storing data till the lifetime of the application? I have read that there is getExternalFiles()
option available from 2.2 but again that is dependent on SDCard. Is there no way an app can store data permanently on Android device?
@everyone-who-says-to-use-internal-storage That file you're talking about will be wiped if you go to Settings>Application>Manage Application>(the app)>Clear Data That is just one method to clear the data stored by your app. This method will wipe everything your app has stored since first run.
The only way to "permanently" store data is to store it on something you have control of, such as a database on your server.
EDIT: Here is simple quick and dirty test case to store and show a saved internal file. This test case is in response to @sunil comment
public class TestFileActivity extends Activity {
private static final String FILENAME = "hello_file";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
byte[] data = null;
String collected = null;
try {
FileInputStream fis = openFileInput( FILENAME );
data = new byte[fis.available()];
while(fis.read(data) != -1) {
collected = new String( data );
}
fis.close();
} catch ( FileNotFoundException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch ( IOException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Button b1 = (Button)findViewById( R.id.button1 );
b1.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View v ) {
putInFile();
}
});
if( collected != null ) {
TextView tv = (TextView)findViewById( R.id.textView1 );
tv.setText( collected );
}
}
private void putInFile() {
String string = "hello world!";
try {
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
} catch ( FileNotFoundException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch ( IOException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></Button>
</LinearLayout>
IF you don't want your data to be cleared in such way, you should probably not use preferences since they can be cleared by the user willingly.
If you choose sdcard as your storage medeium again you are leaving the user with their own option of deleting the contents from their storage.
So you are left with the other option which is using the Internal Memory of your application. You can follow this link for this,
http://developer.android.com/guide/topics/data/data-storage.html
Use a file and write it to internal memory or external storage. Use encryption in case you need it to be secure.
Edit: Some versions of android have an option using which you can delete all the data that is realted to a particular application including the files. Your app must warn the user about this and you must introduce feature to take care of the situation when the file is erased
You can use file system to store in internal sd card
The internal files won't get deleted even after clearing data using "Clear Data". I am going to use the same thing idea in my next project. Idea is --> Store the data in a file using XML format and use the same using XML parser. I hope it works for me and will provide enough hint to use it. Here is a clue how to use internal storage -->
String filename = "file.txt";
FileOutputStream fos;
fos = openFileOutput(filename,Context.MODE_APPEND);
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(fos, "UTF-8");
serializer.startDocument(null, Boolean.valueOf(true));
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
serializer.startTag(null, "root");
for(int j = 0 ; j < 3 ; j++)
{
serializer.startTag(null, "record");
serializer.text(data);
serializer.endTag(null, "record");
}
serializer.endDocument();
serializer.flush();
fos.close();
to read back data using DOM parser:
FileInputStream fis = null;
InputStreamReader isr = null;
fis = context.openFileInput(filename);
isr = new InputStreamReader(fis);
char[] inputBuffer = new char[fis.available()];
isr.read(inputBuffer);
data = new String(inputBuffer);
isr.close();
fis.close();
/*
* converting the String data to XML format
* so that the DOM parser understand it as an XML input.
*/
InputStream is = new ByteArrayInputStream(data.getBytes("UTF-8"));
ArrayList<XmlData> xmlDataList = new ArrayList<XmlData>();
XmlData xmlDataObj;
DocumentBuilderFactory dbf;
DocumentBuilder db;
NodeList items = null;
Document dom;
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
dom = db.parse(is);
// normalize the document
dom.getDocumentElement().normalize();
items = dom.getElementsByTagName("record");
ArrayList<String> arr = new ArrayList<String>();
for (int i=0;i<items.getLength();i++){
Node item = items.item(i);
arr.add(item.getNodeValue());
}
Courtesy : https://stackoverflow.com/a/11690185/842607
精彩评论