Question

I'm using shared preferences to store some images thumbnails, these thumbnails are like the following :

"image:\/\/http%3a%2f%2fimage.tmdb.org%2ft%2fp%2foriginal%2fwBtEVh8Q5dglfjyulGZtgXVy9qi.jpg\/"

and :

"image:\/\/http%3a%2f%2fimage.tmdb.org%2ft%2fp%2foriginal%2fojgf8iJpS4VX6jJfWGLpuEx0wm.jpg\/"

I found that when put some regular Strings into it it works efficiently and I can get it without any problems(that makes me make sure that my code doesn't have any problems), but when putting these thumbnails and trying to get it back it can't find it. So, is there's some String formats that the SharedPreferences don't take?

And this is my code(if needed):

to put :

SharedPreferences sharedPreferences = PreferenceManager
                    .getDefaultSharedPreferences(this);

Editor editor = sharedPreferences.edit();
editor.putString(thumbnail, "some string");
editor.commit();

to get:

String  def_value = sharedPreferences.getString(thumbnail, "value");
Was it helpful?

Solution

In Java any character preceeded by a \ is considered an escape character. For example \n is the escape character representing a new line. In the following string;

"image:\/\/http%3a%2f%2fimage.tmdb.org%2ft%2fp%2foriginal%2fojgf8iJpS4VX6jJfWGLpuEx0wm.jpg\/"

The \/ is an illegal escape character (ie, it doesn't mean anything). So, if you really wanted those two character together what you need is the escape character for back slash (\\) followed by a forward slash, which would look like \\/.

Given that your strings look like urls, I imagine what you are actually looking for doesn't require escape characters at all, and could just look like this.

"image://http%3a%2f%2fimage.tmdb.org%2ft%2fp%2foriginal%2fojgf8iJpS4VX6jJfWGLpuEx0wm.jpg/"

OTHER TIPS

Try with this (from this):

To put:

 // provide the app package name
   SharedPreferences prefs = this.getSharedPreferences(
      "com.example.app", Context.MODE_PRIVATE);
 Editor editor = sharedPreferences.edit();
 editor.putString("thumbnail", "some string");
 editor.commit();

To GET:

SharedPreferences sharedPreferences = getSharedPreferences("com.example.app", Activity.MODE_PRIVATE);
String  def_value = sharedPreferences.getString("thumbnail", "value");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top