Janos
Janos

Reputation: 197

Upload multiple images with sharedpreferences

I am developing an image uploader app. Its working fine with one picture. When I click on the image, i get the media_id and the url where I have to upload the image. This is okay.

When I select multiple images, i store the received response in sharedpref - putString.

the problem is, always the last one is in the sharedpreferences, which is logical. I want to save all the received responses to be able to pass when the upload starts.

I have tried also with putStringSet, but in that case I cant pass it when it comes to the upload because its waiting for string.

what would be a simple solution for this?

String media_id = response.getString("media_id");
String content_url = response.getString("content_url");

SharedPreferences sharedPref1 = getSharedPreferences("server", Context.MODE_PRIVATE);
SharedPreferences.Editor editor2 = sharedPref1.edit(); 

editor2.putString("content_url", content_url);
editor2.putString("media_id", media_id);

If i have multiple images, i got multiple responses, (more than just one media_id for example).

This is how i receive them:

SharedPreferences sharedPref1 = getSharedPreferences("server", Context.MODE_PRIVATE);

        String content_url1 = sharedPref1.getString("content_url", "");

        String media_id1 = sharedPref1.getString("media_id", "");

How to save multiple and pass them later? Thanks.

Upvotes: 0

Views: 130

Answers (1)

farhan patel
farhan patel

Reputation: 1610

You can convert it to JSON String and store the string in the shared preferences.

"images": [{
    "media_id": "some id for image1",
    "content_url": "some url for image1",

}, {
    "media_id": "some id for image2",
    "content_url": "some url for image3",

},
        {
  "media_id": "some id for image3",
  "content_url": "some url for image3",

   },

]

Upvotes: 1

Related Questions