我怎么能存储的图像使用共享preference android系统中? [英] How can I store images using sharedpreference in android?

查看:122
本文介绍了我怎么能存储的图像使用共享preference android系统中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要保存图像的机器人使用共享preference。我有两个活动课,当我点击第一个活动的按钮,它会调用第二个活动,第二个活动显示我的$ P $列表视图pferred名,并且也重置了Android墙纸,我已经设置为$图像p $ pferred壁纸的第一个活动。

I want to save images in android using sharedpreference. I have two activity classes, when i click the button of first activity it will call the second activity and second activity displays my preferred name in a list view and also resets the android wallpaper to the image that I had set as a preferred wallpaper in the first activity.

对于第二个活动code是:

For the second activity the code is:

public class PreferencesActivityTest extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


            SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
            String prefName = myPrefs.getString("PREF_USERNAME", "nothing");
            String wallPaper = myPrefs.getString("PREFS_NAME", null);


            if(wallPaper != null) {

                try {

                      Bitmap bm = BitmapFactory.decodeFile("/data/misc/wallpaper/"+wallPaper);
                      Log.d(getClass().getSimpleName(),"Wallpaper name is: "+ wallPaper);
                      setWallpaper(bm);
                      Toast.makeText(this, "Wall paper has been changed." +
                                  "You may go to the home screen to view the same", Toast.LENGTH_LONG).show();
                } 

                catch (FileNotFoundException fe){
                      Log.e(getClass().getSimpleName(),"File not found");
                } catch (IOException ie) {
                      Log.e(getClass().getSimpleName()," IO Exception");
                }

    }


        ArrayList<String> results = new ArrayList<String>();
        results.add("Your Preferred name is: " + prefName);
      this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
    }

第一个活动调用第二个活动,但他并没有叫如果(壁纸!= NULL){}

为什么不工作?

推荐答案

它不是在分享preferences推荐店铺形象你应该存储图像sdcard.And然后存储图像路径(从SD卡)为共享preferences这样的 -

Its not recommended to store image in Share preferences And you should store that image to sdcard.And then store image path (from sdcard) into Share preferences like this--

    SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
    Editor edit=shre.edit();
    edit.putString("imagepath","/sdcard/imh.jpeg");
    edit.commit();

,然后通过这条道路,从取SD卡图像

and then fetch image from sdcard by using this path

这篇关于我怎么能存储的图像使用共享preference android系统中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆