28

The exception only occurs in Android 7.0 Nougat (emulator) devices.

java.lang.SecurityException: MODE_WORLD_READABLE no longer supported

My code:

public void SessionMaintainence(Context context) {
    this.context = context;
    preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_WORLD_READABLE);
    editor = preferences.edit();
    editor.commit();
}

LogCat:

> E/AndroidRuntime: FATAL EXCEPTION: main
>                                                  Process: burpp.av.feedback, PID: 2796
>                                                  java.lang.RuntimeException: Unable to create application
> burpp.av.feedback.FeedbackApplication: java.lang.SecurityException:
> MODE_WORLD_READABLE no longer supported
>                                                      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5364)
>                                                      at android.app.ActivityThread.-wrap2(ActivityThread.java)
>                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
>                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
>                                                      at android.os.Looper.loop(Looper.java:154)
>                                                      at android.app.ActivityThread.main(ActivityThread.java:6077)
>                                                      at java.lang.reflect.Method.invoke(Native Method)
>                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
>                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
>                                                   Caused by: java.lang.SecurityException: MODE_WORLD_READABLE no longer supported
>                                                      at android.app.ContextImpl.checkMode(ContextImpl.java:2162)
>                                                      at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:363)
>                                                      at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:358)
>                                                      at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:164)
>                                                      at burpp.av.feedback.support.SessionMaintainence.<init>(SessionMaintainence.java:63)
>                                                      at burpp.av.feedback.FeedbackApplication.onCreate(FeedbackApplication.java:43)
>                                                      at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024)
Nandkishor mewara
  • 2,552
  • 16
  • 29
Abdul Gaffar
  • 581
  • 2
  • 7
  • 16
  • look into the documentation, MODE_WORLD_READABLE is deprecated because it´s too dangerous.: https://developer.android.com/reference/android/content/Context.html#MODE_WORLD_READABLE – Opiatefuchs Aug 24 '16 at 10:43
  • 4
    As the error indicates, `MODE_WORLD_READABLE` is not supported. Remove that flag from your `getSharedPreferences()` call, replacing it with `MODE_PRIVATE` or `0`. – CommonsWare Aug 24 '16 at 10:44
  • There is a huge demand of 'MODE_WORLD_READABLE' in my application. And its been working fine till 6.0 but its not working with 7.0 – Abdul Gaffar Aug 24 '16 at 11:05
  • 1
    if i use `MODE_PRIVATE` then i cant able to share some datas to other application. and if i use `MODE_WORLD_READABLE` it does allows to share some datas but it is not secured. So what should i do in this case. – Abdul Gaffar Aug 24 '16 at 11:23
  • What if I want to access shared preference value in another app? Have you any find out any solution for it? – Anand Savjani Mar 05 '19 at 18:32

1 Answers1

42

World-readable files can be a security flaw. So android first deprecated it and then completely removed it. MODE_WORLD_READABLE was deprecated in versions till Android M. But in Android N it is no longer supported and throws SecurityException. So try a different mode. I used Context.MODE_PRIVATE and it worked.

Rakesh Yadav
  • 1,966
  • 2
  • 21
  • 35
  • 1
    Is this also applicable to **getDefaultSharedPreferences**? – IgorGanapolsky Dec 07 '16 at 14:28
  • the method is context.getSharedPreferences(PREF_NAME, MODE), I don't find getDefaultSharedPreferences(). If it is getSharedPreferences() what you are asking for, then the answer is yes, because we preference name and MODE (which was asked in the question). – Rakesh Yadav Dec 08 '16 at 03:54
  • @R.Y he's referring to [`PreferenceManager.getDefaultSharedPreferences(Context context)`](https://developer.android.com/reference/android/preference/PreferenceManager.html#getDefaultSharedPreferences(android.content.Context)). – Sufian Dec 08 '16 at 06:30
  • 1
    @IgorGanapolsky I think default prefs use private mode. You will either have to check the AOSP source code or maybe someone has already asked it on the internet. Anyway, since the SharedPreference is created by the framework itself, we can assume that this exception will not occur with `getDefaultSharedPreferences()`. – Sufian Dec 08 '16 at 06:37
  • 1
    Context.MODE_PRIVATE has worked for me too. thanks @RakeshYadav – Samuel Owino Apr 30 '18 at 08:39
  • @IgorGanapolsky yes – Rajesh N May 27 '18 at 11:49
  • But what do they want if I need to make the file available for reading, for example, as a webview element? – Юрій Писанка Jul 31 '22 at 21:04