I am trying to save the backgroundImage of the ViewPager, using SharedPreferences and load that image as background when we run the app for the next time. It works fine in the emulator but not in the device. Please help me in solving this.Thanks,

Here is my code..

 SlidePagerActivity.mPager.setBackgroundResource(R.drawable.snow);

    SharedPreferences backgroundImage = getSharedPreferences("BGPREFS", MODE_WORLD_READABLE);
    SharedPreferences.Editor bgEditor = backgroundImage.edit();
    bgEditor.putInt("imgValue", R.drawable.app_bg_snowfall);
    bgEditor.commit();
有帮助吗?

解决方案

Try this:

To put in SharedPreferences

  SharedPreferences sp = getSharedPreferences("myApp", Activity.MODE_PRIVATE);  
    SharedPreferences.Editor editor = sp.edit();  
    editor.putInt("NewBackground", idImage);  
    editor.commit(); 

To get the Preference:

SharedPreferences sp = getSharedPreferences("myApp", Activity.MODE_PRIVATE);
int myNewIntValue = sp.getInt("NewBackground", 0);

and finally use it. MyNewIntValue

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top