android externalising a method containing an intent -


i have method duplicated in more 1 of main activities in application, , externalize run form utility activity, don't have mess of code showing in main activity, contains alertdialog switch , runs relevant intent. unsure of best way done.

what result of string "imagepath" utility activity , return main activity after selected intent has run.

i have been able pass context alertdialog.builder display alert, don't no how intents run.

main activity:

imagepath = utility.get(this); or intent intent1 = new intent(utility.get(this)); startactivity(intent1); 

utility activity:

public static void get(context context) {  //some code , context start dialog , run intent.  return imagepath; } 

methods need run in utility activity.

// select image dialog      public void selectimage() {         alertdialog.builder select = new alertdialog.builder(this);         select.settitle(r.string.selectimage);         select.setitems(r.array.imagefrom,                  new dialoginterface.onclicklistener() {                    public void onclick(dialoginterface dialog, int which) {                        switch(which){                        case 0: // take photo                         takepictureintent(0);                            break;                        case 1: // load gallery                          getpictureintent(1);                            break;                        }                }         }).show();     }  private void takepictureintent(int actioncode){     intent takepictureintent = new intent(mediastore.action_image_capture);      // create file save image           imageuri = getoutputmediafileuri(0);      imagepath = imageuri.getencodedpath(); // set image file name           takepictureintent.putextra(mediastore.extra_output, imageuri);  // start camera      startactivityforresult(takepictureintent, 0); }  private static uri getoutputmediafileuri(int type){       return uri.fromfile(getoutputmediafile(type)); }  @suppresslint("simpledateformat") private static file getoutputmediafile(int type){     file mediastoragedir = new file(environment.getexternalstoragepublicdirectory(               environment.directory_pictures), "pictures");     if (! mediastoragedir.exists()){         if (! mediastoragedir.mkdirs()){             log.d("camera", "failed create directory");             return null;         }     }     string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date());     file mediafile = new file(mediastoragedir.getpath() + file.separator + "img_" + timestamp + ".jpg");     return mediafile; }  public void getpictureintent(int actioncode) {     intent getpictureintent = new intent(intent.action_pick,              android.provider.mediastore.images.media.external_content_uri);            startactivityforresult(getpictureintent, 1);     }  @override protected void onactivityresult(int requestcode, int resultcode, intent data) {         if (resultcode == result_ok)             switch(requestcode){                 case 0: // image captured , saved  // toast saved path                         toast.maketext(this, "image saved to:\n" + imagepath, toast.length_long).show();                     break;                 case 1: // image selected                         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]);                         imagepath = cursor.getstring(columnindex);                         cursor.close(); // image selected                                         break;         } } 

if 1 can point me in right direction awesome. cheers.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -