문제

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