Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store images using SharedPreference in android?

Tags:

android

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?

like image 510
Laxmipriya Avatar asked Dec 05 '11 12:12

Laxmipriya


People also ask

How do I save pictures from internal storage on Android?

This example demonstrates How to write an image file in internal storage in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How much data can be stored in SharedPreferences Android?

There is a limitations of SharedPreference data. In my case it throw a Memory Exception when SharedPreference data cross 1428.51-kb. So its better to use SQLite database when you required huge data to store.


1 Answers

All you have to do is, convert your image to it's Base64 string representation:

Bitmap realImage = BitmapFactory.decodeStream(stream); ByteArrayOutputStream baos = new ByteArrayOutputStream(); realImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);    byte[] b = baos.toByteArray();   String encodedImage = Base64.encodeToString(b, Base64.DEFAULT); textEncode.setText(encodedImage);  SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this); Editor edit=shre.edit(); edit.putString("image_data",encodedImage); edit.commit(); 

and then, when retrieving, convert it back into bitmap:

SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this); String previouslyEncodedImage = shre.getString("image_data", "");  if( !previouslyEncodedImage.equalsIgnoreCase("") ){     byte[] b = Base64.decode(previouslyEncodedImage, Base64.DEFAULT);     Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);     imageConvertResult.setImageBitmap(bitmap); } 

However, I have to tell you that Base64 support is only recently included in API8. To target on lower API version, you need to add it first. Luckily, this guy already have the needed tutorial.

Also, I've created quick and dirty example on github.

like image 119
ariefbayu Avatar answered Sep 22 '22 15:09

ariefbayu