How to store an image using sharedpreferences? Android -


my code below put image gallery imagebutton when leave application or move activity image don't save , first background appear again.

i need help, how can save image define imagebutton background

i read sharedpreferences, don't know how use on app

-

- class

   protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);       //adding picture bit         imgbutton = (imagebutton) findviewbyid(r.id.addpic);     imgbutton.setonclicklistener(new onclicklistener() {         @override         public void onclick(view v) {             intent galeryintent = new intent(intent.action_pick, android.provider.mediastore.images.media.external_content_uri);             startactivityforresult(galeryintent, result_load_image);         }     }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) {      super.onactivityresult(requestcode, resultcode, data);     if (requestcode == result_load_image && resultcode == result_ok && null != data) {         uri selectedimage = data.getdata();         string[] filepathcolumn = {mediastore.images.media.data };          cursor selectedcursor = getcontentresolver().query(selectedimage, filepathcolumn, null, null, null);         selectedcursor.movetofirst();          int columnindex = selectedcursor.getcolumnindex(filepathcolumn[0]);         string picturepath = selectedcursor.getstring(columnindex);         selectedcursor.close();        //  drawable d = new bitmapdrawable(getresources(),bitmapfactory.decodefile(picturepath));         // btnopengalery .setimagebitmap(d);         imgbutton.setimagebitmap(bitmapfactory.decodefile(picturepath));         toast.maketext(getapplicationcontext(), picturepath, toast.length_short).show();      }  }    

my xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:gravity="center"     android:orientation="vertical" >  <imagebutton     android:id="@+id/addpic"     android:layout_width="100dp"     android:layout_height="100dp"     android:layout_gravity="center"     android:gravity="left"     android:onclick="addpic"     android:background="@drawable/ic_launcher" />  </linearlayout> 

if want use sharedpreferences, use below code:

  sharedpreferences sharedpreferences;  protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); sharedpreferences = getsharedpreferences("data",  context.mode_private);    //adding picture bit      imgbutton = (imagebutton) findviewbyid(r.id.addpic); imgbutton.setonclicklistener(new onclicklistener() {      @override      public void onclick(view v) {         intent galeryintent = new intent(intent.action_pick, android.provider.mediastore.images.media.external_content_uri);         startactivityforresult(galeryintent, result_load_image);     }  });   if(sharedpreferences!=null) string path = sharedpreferences.getstring("path", null); if(path!=null)   imgbutton.setimagebitmap(bitmapfactory.decodefile(path));     }  @override  protected void onactivityresult(int requestcode, int resultcode, intent data) {  super.onactivityresult(requestcode, resultcode, data); if (requestcode == result_load_image && resultcode == result_ok && null != data) {     uri selectedimage = data.getdata();     string[] filepathcolumn = {mediastore.images.media.data };      cursor selectedcursor = getcontentresolver().query(selectedimage, filepathcolumn, null, null, null);     selectedcursor.movetofirst();      int columnindex = selectedcursor.getcolumnindex(filepathcolumn[0]);     string picturepath = selectedcursor.getstring(columnindex);     selectedcursor.close();     sharedpreferences.editor editor = sharedpreferences.edit();     editor.putstring("path", picturepath);     editor.commit();    //  drawable d = new bitmapdrawable(getresources(),bitmapfactory.decodefile(picturepath));      // btnopengalery .setimagebitmap(d);      imgbutton.setimagebitmap(bitmapfactory.decodefile(picturepath));     toast.maketext(getapplicationcontext(), picturepath, toast.length_short).show();  }  

}


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -