1

I am currently making an android app that changes the device wallpaper based on weather conditions. I want the user to be able to select images from their device which will then be used for the wallpaper. Unfortunately, whenever I close the app the selected images disappear from the image view. Is there any way I can save the images within my app even when my app is closed?

Sorry if this is a basic question. This is my first app. : )

2 Answers2

0

I suggest you use the native Android preferences.

You could set the image that follows the weather directly with them. For example, if the weather is rain, set the rain preference as active and show the rain image using it. Similarly, if the weather is snow, disable the rain preference and enable the snow preference and so on.

You can also save the pictures through preferences. (you could also use ROOM, but if your purpose is to save images, well, a database is not the "correct" solution).

Documentation: Android Shared Preferences

Useful links: How to save Image in shared preference in Android

gcantoni
  • 727
  • 6
  • 13
0

You have two things you need to do really.

The first one is persisting your state - basically, you need to be able to store the information you're holding (references to images, any ordering they have, which one is associated with which weather type etc), and then restore it when the app opens again.

Here's the documentation overview for different ways of storing data, but there's two main options - some kind of database, or SharedPreferences. SharedPreferences sounds like it's about preferences, but it's really a simple key/value store for things like Strings, ints, mayyybe more complicated structures if you serialise them (you probably don't need to worry about that). Here's how you use it, and if you want to save a bunch of file paths as strings that might be your best bet


The other thing you need to worry about is access to the actual images. Here's how you can open a system file picker, and here's some details about giving yourself permanent access, so you don't lose it after reboots. This might be a little complicated, so you might want to consider importing the images, saving copies to your app's own storage space (see the first link) - basically, once you have your own copies, you can read and write them as you like and you can just store File paths as strings, so saving and restoring is pretty simple. You need to handle cleaning them up when they're not in use anymore though!

cactustictacs
  • 17,935
  • 2
  • 14
  • 25