Pregunta

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();
¿Fue útil?

Solución

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top