android - How to set app background from phone gallery -


i'm sure there answer out there somewhere can't seem find anything. trying make when user clicks on item on app menu start intent phone's gallery choose image of choice change background of app rather pre-set background image. there way of doing this.

here code have when run on phone, app crashes.

@override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     intent sendintent = null;     if (id == r.id.action_settings) {           sendintent = new intent();         sendintent.setaction(intent.action_send);         sendintent.putextra(intent.extra_text, "try out new app...");         sendintent.settype("text/plain");         startactivity(sendintent);     }       packagemanager packagemanager = getpackagemanager();     list<resolveinfo> activities =                   packagemanager.queryintentactivities(sendintent, 0);     boolean isintentsafe = activities.size() > 0;       string title = getresources().getstring(r.string.chooser_title);      intent chooser = intent.createchooser(sendintent, title);       if (sendintent.resolveactivity(getpackagemanager()) != null) {         startactivity(chooser);     }        //noinspection simplifiableifstatement      if (id == r.id.change_background){     intent pictureintent = new intent();     pictureintent.settype("image/*");     pictureintent.setaction(intent.action_get_content);     startactivityforresult(intent.createchooser(pictureintent,  

"select picture"), 0);

}      return true; 

}}

to select image gallery

try {                 intent intent = new intent();                 intent.settype("image/*");                 intent.setaction(intent.action_get_content);                 startactivityforresult(intent.createchooser(intent, "select picture"),  pick_image);             } catch (exception e) {                 toast.maketext(getapplicationcontext(),e.getmessage(),toast.length_long).show();                 log.e(e.getclass().getname(), e.getmessage(), e);             } 

and in onactivityresult

@override protected void onactivityresult(int requestcode, int resultcode, intent data) {      switch (requestcode) {     case pick_image:          if (resultcode == activity.result_ok) {             uri selectedimageuri = data.getdata();             try {                 // oi file manager                 string filemanagerstring = selectedimageuri.getpath();                  // media gallery                 string selectedimagepath = getpath(selectedimageuri);                  if (selectedimagepath != null) {                     filepath = selectedimagepath;                 } else if (filemanagerstring != null) {                     filepath = filemanagerstring;                 } else {                     toast.maketext(getapplicationcontext(), "unknown path",                             toast.length_long).show();                  }                  if (filepath != null) {                               decodefile(filepath);                 } else {                     bitmap = null;                 }             } catch (exception e) {                 toast.maketext(getapplicationcontext(), "internal                        error",toast.length_long).show();              }         }         break;     default:     }    }        //decode file() decoded file , can use image       //file according requirement     public void decodefile(string filepath) {      // decode image size     bitmapfactory.options o = new bitmapfactory.options();     o.injustdecodebounds = true;     bitmapfactory.decodefile(filepath, o);      // new size want scale     final int required_size = 1024;      // find correct scale value. should power of 2.     int width_tmp = o.outwidth, height_tmp = o.outheight;     int scale = 1;     while (true) {         if (width_tmp < required_size && height_tmp < required_size)             break;         width_tmp /= 2;         height_tmp /= 2;         scale *= 2;       }      // decode insamplesize     bitmapfactory.options o2 = new bitmapfactory.options();     o2.insamplesize = scale;     bitmap = bitmapfactory.decodefile(filepath, o2);      image.setimagebitmap(bitmap);// set imageview in case set layout                 //background  } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -