我想使用sharedpreference在android中保存图像.我有两个活动类,当我点击第一个活动的按钮时,它将调用第二个活动,第二个活动在列表视图中显示我的首选名称,并且还将android壁纸重置为我已设置为首选壁纸的图像.第一次活动.
对于第二个活动,代码是:
public class PreferencesActivityTest extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String prefName = myPrefs.getString("PREF_USERNAME", "nothing");
String wallPaper = myPrefs.getString("PREFS_NAME", null);
if(wallPaper != null) {
try {
Bitmap bm = BitmapFactory.decodeFile("/data/misc/wallpaper/"+wallPaper);
Log.d(getClass().getSimpleName(),"Wallpaper name is: "+ wallPaper);
setWallpaper(bm);
Toast.makeText(this, "Wall paper has been changed." +
"You may go to the home screen to view the same", Toast.LENGTH_LONG).show();
}
catch (FileNotFoundException fe){
Log.e(getClass().getSimpleName(),"File not found");
} catch (IOException ie) {
Log.e(getClass().getSimpleName()," IO Exception"); …Run Code Online (Sandbox Code Playgroud) android ×1