I am trying to figure out how I would achieve storing image URLs via onClick of an item button so they can be accessed by another class.

I have looked around and saw that it would be best to achieve this using shared preferences.

I have never used shared preferences before so I am a little confused as how I will be able to achieve this because I would like to get the URL from the String I have called "mImageUrl"

I know my String "mImageUrl" will give me the URL of the image that is currently being viewed, so I like to somehow store the String/URL from my String to shared preferences so the specific URLs can used via another class.

Would using shared prefs be a good way to achieve my requirement, Any guidance would be appreciated thanks

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.SetWallpaper:
        new SetWallpaperAsync(getActivity()).execute(mImageUrl);

        break;


    case R.id.SaveWallpaper:
        new SaveWallpaperAsync(getActivity()).execute(mImageUrl);

        break;

    case R.id.FavouriteWallpaper:
        //Use shared preferences here somehow:


                SharedPreferences preferences = this.getActivity().getSharedPreferences("pref",0);

        SharedPreferenceUtil.setSharedPreference(context, "ImageKey", mImageUrl);

        String url = SharedPreferenceUtil.getSharedPreference(context,"ImageKey",null);

        if(url != null){
           // set image source here..
        }

        break;

    }
    return super.onOptionsItemSelected(item);
}
有帮助吗?

解决方案

Try this code:

Save in SharedPreferences :

SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(contextActivity);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("imgUrl", UrlString);
editor.commit();

To retrive value:

prefs = PreferenceManager.getDefaultSharedPreferences(contextActivity);
prefs.getString("imgUrl", null);

其他提示

try this in one Activity :

SharedPreferences sp;
SharedPreferences.Editor edit;
sp = getSharedPreferences("enter", MODE_PRIVATE);
edit = sp.edit();
edit.putString("imagerul", user_validate);
edit.commit();

in next activity :

SharedPreferences sp = getSharedPreferences("enter", MODE_PRIVATE);
sp.getString("imageurl", "fdgf"));

You can simple implement a class that handles get/set operations of shared preferences.

I will provide a class so that you can easily integrate to your project.

Here is our SharedPreferenceUtil class

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferenceUtil {

    public static String getSharedPreference(Activity activity, String prefName, String key, String defaultValue){
        SharedPreferences prefs = activity.getSharedPreferences(prefName,0);
        String pref = prefs.getString(key, defaultValue);
        return pref;
    }

    public static void setSharedPreference(Activity activity, String prefName, String key, String value){
        SharedPreferences prefs = activity.getSharedPreferences(prefName,0);
        SharedPreferences.Editor editor = prefs.edit();

        // edit and commit
        editor.putString(key, value);
        editor.commit();
    }
} 

You can simply set/get preference from your activity with the following code sample.

// You can set your shared preferences like following.
SharedPreferenceUtil.setSharedPreference(this.getActivity(),"pref","yourImageKey","yourImageUrl");

// and you can get your shared preferences like following.
String url = SharedPreferenceUtil.getSharedPreference(this.getActivity(),"pref","yourImageKey",null);

if(url != null){

}

EDIT:

You can reach SharedPreferences from a Fragment by following

SharedPreferences preferences = this.getActivity().getSharedPreferences("pref",0);

Hope this may help.

Try:

SharedPreferences pr=PreferenceManager.getDefaultSharedPreferences(context);

SharedPreferences.Editor r=pr.edit();

r.putString("name","yourlink");

r.commit();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top