Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save data till application un-install in android?

Tags:

android

save

In Android the SharedPreference and SQLite databases are cleared once the user clear's data 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?

like image 411
sunil Avatar asked Feb 24 '23 13:02

sunil


2 Answers

@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>
like image 170
rf43 Avatar answered Mar 12 '23 04:03

rf43


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

like image 38
Andro Selva Avatar answered Mar 12 '23 04:03

Andro Selva