Domanda

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();
È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top