-1

I'm my ProfileFragment.java

I have a button which allows the users to change their profile photo and their banner. When the user clicks in "change banner" he gets redirected to the gallery where he can choose the photo he wants and then, once it is chosen, the photo changes using Picasso

Picasso.with(getContext()).load(imageUri).resize(446, 203).into(imgFotoCapa);)

The problem is, this only lasts while the user doesn't close the app... Once the user closes it and then, later, reopens it, the banner goes back to the default one.

How can I store the imageUri that comes from the gallery so I can retrieve it later and every time the user enters ProfileFragment.java the photo goes back to the banner?

Thanks in advance!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
André
  • 25
  • 9

1 Answers1

0

What's happening in your case is that the image isn't being saved and once the fragment is destroyed, the image data is also destroyed.

If you are only saving one uri, or simple stuff, you can use SharedPreferences (have a look at this answer to save uri in the SharedPreferences). On the other hand, if you are looking to store more complex stuff, I would recommend using a database. Have look at Realm docs, or even Room persistency here.

miguelarc
  • 791
  • 7
  • 13
  • Well, I've tried the way that is answered in the question you sent and had no success... It says "Incompatible types" when I try to retrieve – André Jun 07 '18 at 10:09
  • When you get back at the fragment, you need to access SharedPreferences again, to load the image. Why isn't the answer you tried working? Have you debugged to see if the image is loading in code? – miguelarc Jun 07 '18 at 10:11
  • Well, to retrieve I've used: ` SharedPreferences sp2 = getContext().getSharedPreferences("FotosCapa", Context.MODE_PRIVATE); try { imageUri = URI.create(sp2.getString("url", "defaultString")); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); }` and I had no success – André Jun 07 '18 at 10:11
  • The "Incompatible types" might be hapening cause the uri is being saved as a String. Have you tried to convert it? – miguelarc Jun 07 '18 at 10:12
  • No, I can't even run my app because it gives that underline red color error – André Jun 07 '18 at 10:12
  • Isnt this `imageUri = URI.create(myPrefs.getString("url", "defaultString"));` converting already to URI when I retrieve the data? – André Jun 07 '18 at 10:13
  • Well, it says that `Required: android.net.URI` and `Found: java.net.Uri` – André Jun 07 '18 at 10:15
  • Probably because you are using the wrong import. Try the android one. – miguelarc Jun 07 '18 at 10:17
  • Where? When I store the data? – André Jun 07 '18 at 10:20
  • Well, I've tried: try { imageUri = Uri.parse(sp2.getString("url", "defaultString")); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } and then `Picasso.with(getContext()).load(imageUri).into(imgFotoCapa);` and still got a crash – André Jun 07 '18 at 10:21
  • the error comes from ` imageUri = Uri.parse(sp2.getString("url", "defaultString")); ` – André Jun 07 '18 at 10:26
  • Try this to do the parse: `SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); String imageUriString = settings.getString("url", null); Uri imageUri = Uri.parse(imageUriString ); <-- parse` – miguelarc Jun 07 '18 at 10:27
  • But this is when I store the data or when I retrieve it? – André Jun 07 '18 at 10:28
  • The parse is only for the uri load. When you save it, it saves as a string, so you need to convert it back to uri when you load it. – miguelarc Jun 07 '18 at 10:32
  • I've debugged and the error comes from `Uri imageUri = Uri.parse(imageUriString);` because when it runs that line, the app crashes – André Jun 07 '18 at 10:44
  • I've found the problem. Everything works fine now but when I run the app the image is not displayed and instead of displaying the default image it gets blank – André Jun 07 '18 at 10:59
  • All you need to do now is setting the image view content based on you uri, using the `setImageUri()` method. Have a look at [this](https://stackoverflow.com/a/9080762/7671879). – miguelarc Jun 07 '18 at 13:05
  • I've tried both that and Picasso and no image is displayed... I've debugged and the imageUri is not null... it's link is `content://` ect – André Jun 07 '18 at 13:19
  • Try creating a bitmap of the image instead, and then apply the bitmap to the imageView. Use [this](https://stackoverflow.com/a/4717740/7671879) to create the bitmap from the uri, and [this](https://stackoverflow.com/a/2930648/7671879) to set the bitmap to the imageview. – miguelarc Jun 07 '18 at 13:22