I want to save images in android using SharedPreference. I have two activity classes, when I click the button of the first activity it will call the second activity and the second activity displays my preferred name in a list view and also resets the android wallpaper to the image that I had set as a preferred wallpaper in the first activity.
For the second activity the code is:
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");
}
}
ArrayList<String> results = new ArrayList<String>();
results.add("Your Preferred name is: " + prefName);
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
}
The first activity calls the second activity but it is not calling if(wallPaper != null){}
Why isn’t it working?
Its not recommended to store image in Share preferences And you should store that image to sdcard.And then store image path (from sdcard) into Share preferences like this–
and then fetch image from sdcard by using this path