android - How to save an image picked from gallery to shared preferences -


i have made application has background image set. want pick image gallery , set application background. part done. want picked image sets permanently application background, because reopen application default image gets set.

how can save selected image permanently till try change again?

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     bn= (button) findviewbyid(r.id.button);     sharedpreferences sp = getsharedpreferences("student", mode_private);     final sharedpreferences.editor spedit = sp.edit();     v = r.drawable.back;     spedit.putint("background", v);     relativelayout bg = (relativelayout) findviewbyid(r.id.abc);     bg.setbackgroundresource(v);      bn.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             intent = new intent(                     intent.action_pick,                     android.provider.mediastore.images.media.external_content_uri);              startactivityforresult(i, 101);         }     }); }  @override protected void onactivityresult(int requestcode, int resultcode, intent data) {     super.onactivityresult(requestcode, resultcode, data);      if (requestcode == 101 && resultcode == result_ok && null != data) {         uri selectedimage = data.getdata();         string[] filepathcolumn = { mediastore.images.media.data };          cursor cursor = getcontentresolver().query(selectedimage,                 filepathcolumn, null, null, null);         cursor.movetofirst();          int columnindex = cursor.getcolumnindex(filepathcolumn[0]);         string picturepath = cursor.getstring(columnindex);         cursor.close();         bitmap = bitmapfactory.decodefile(picturepath);         drawable d = new bitmapdrawable(getresources(),bitmap);         relativelayout bg = (relativelayout) findviewbyid(r.id.abc);         bg.setbackground(d);     } } 

i want when app runs first time default background seen , change background image should seen in further launches.

at below have assigned our picture address shared preferences.after in oncreate section have checked if address valid.according changed background.

 sharedpreferences sp;   @override protected void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             setcontentview(r.layout.activity_main);             bn= (button) findviewbyid(r.id.button);             sp = getsharedpreferences("student", mode_private);               string savedpicturepath = sp.getstring("imagepath","null");              if(!savedpicturepath.equals("null"){              bitmap = bitmapfactory.decodefile(savedpicturepath);                 drawable d = new bitmapdrawable(getresources(),bitmap);                 relativelayout bg = (relativelayout) findviewbyid(r.id.abc);                 bg.setbackground(d);             }             bn.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view v) {                     intent = new intent(                             intent.action_pick,                             android.provider.mediastore.images.media.external_content_uri);                      startactivityforresult(i, 101);                  }             });           }              @override                   protected void onactivityresult(int requestcode, int resultcode,    intent data) {                 super.onactivityresult(requestcode, resultcode, data);              if (requestcode == 101 && resultcode == result_ok && null != data) {                 uri selectedimage = data.getdata();                 string[] filepathcolumn = { mediastore.images.media.data };                  cursor cursor = getcontentresolver().query(selectedimage,                         filepathcolumn, null, null, null);                 cursor.movetofirst();                  int columnindex = cursor.getcolumnindex(filepathcolumn[0]);                 string picturepath = cursor.getstring(columnindex);                  sp.edit().putstring("imagepath",picturepath);                 cursor.close();                 bitmap = bitmapfactory.decodefile(picturepath);                 drawable d = new bitmapdrawable(getresources(),bitmap);                 relativelayout bg = (relativelayout) findviewbyid(r.id.abc);                 bg.setbackground(d);               }           }          } 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -