11

I want to know which is better for storage purpose: Shared preferences or internal file storage. Which is faster? Is there any memory issue with anyone of them?

Thanks,

Manjeet Singh
  • 4,382
  • 4
  • 26
  • 39
Ram
  • 141
  • 1
  • 5
  • why don't you make a test to see which one is faster? and regarding memory issues, if your code is good, you won't have any issues with any of both methods – Buda Gavril Jun 27 '12 at 08:53

4 Answers4

14
  • SharedPreference: Store private primitive data in key-value pairs. (To store small entries/data)
  • Internal Storage: Store private data on the device memory. (To store large datasets)

More about Storage Options here: Storage Options

I have seen an issue about shared preference.

Issue: Whenever you face Force close/app crash while using SharedPreference, SharedPreference will be null. Here you can read more about it: Shared Preferences reset when the app is force closed or crashes

Community
  • 1
  • 1
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • +1. Good Answer. I have a question. Can Shared Preferences's XML file be exported and view by somebody having the phone? I ask this because I intend to store the current user's username and password in the shared preferences – Ashwin Sep 04 '12 at 10:33
  • @Ashwin check: (getSharedPreferences with MODE_PRIVATE)[http://developer.android.com/reference/android/content/Context.html#getSharedPreferences(java.lang.String, int)] – Paresh Mayani Sep 04 '12 at 10:49
  • MODE_PRIVATE says that other applications cannot access the shared preference. But can I connect my phone to the PC and go to DDMS(Eclipse) and from there export this file to the PC. (It is possible to do so for DATABASE - DB can be exported to the PC) – Ashwin Sep 04 '12 at 12:32
3

Well, the Shared preferences also saved in the internal file storage at last.

the Share preferences like the key-value pair, it keep the data in memory, and saved to the internal file storage by system at last.

if you want to access the value frequently, you can use Shared preferences.

if you data structure is small and not complicated, you can directed use Share preferences

if you just want to save data, and the data is will taken many memory space, you can just use file to save.

idiottiger
  • 5,147
  • 2
  • 25
  • 21
  • Can Shared Preferences's XML file be exported and view by somebody having the phone? I ask this because I intend to store the current user's username and password in the shared preferences. – Ashwin Sep 04 '12 at 10:32
1

Please read Android document related to data storage.

If you want to store very small values then you can opt for SharedPrefrence.

rajpara
  • 5,203
  • 1
  • 29
  • 46
0

It totally depends on what you like to do: If you have a rather small amount of key-value pairs go with the Shared Preferences. If you have a larger amount of data use a file as the Shared Preferences will be hold in memory after they where first used. So it's a bad idea to store large amount of data in it ;-).

Steven Mohr
  • 1,167
  • 6
  • 19