android - Image Crop Not Working in Fragment -


below code working nic when using activity when implemet code in fragment after select image gallery or capture camera not showing crop option. image not showing in imageview.

below camera handler:

public class camerahandler {     public static int image_pic_code = 1000, crop_camera_request = 1001,             crop_gallary_request = 1002;     private intent imagecaptureintent;     private boolean isactivityavailable;     context context;     private list<resolveinfo> cameralist;     list<intent> cameraintents;     uri outputfileuri;     intent galleryintent;     uri selectedimageuri;     private string cameraimagefilepath, absolutecameraimagepath;     bitmap bitmap;     imageview ivpicture;      string ivpicture1 = string.valueof(ivpicture);      public camerahandler(context context) {         this.context = context;         setfileuriforcameraimage();     }      public void setivpicture(imageview ivpicture) {         this.ivpicture = ivpicture;     }      private void setfileuriforcameraimage() {         file root = new file(environment.getexternalstoragedirectory()                 + file.separator + "mydir" + file.separator);         root.mkdirs();         final string fname = "image.jpg";         final file sdimagemaindirectory = new file(root, fname);         absolutecameraimagepath = sdimagemaindirectory.getabsolutepath();         outputfileuri = uri.fromfile(sdimagemaindirectory);     }      public string getcameraimagepath() {         return cameraimagefilepath;     }      private void getactivities() {         imagecaptureintent = new intent(mediastore.action_image_capture);         packagemanager packagemanager = ((activity) context)                 .getpackagemanager();         this.cameralist = packagemanager.queryintentactivities(                 imagecaptureintent, 0);         if (cameralist.size() > 0) {             isactivityavailable = true;         } else {             isactivityavailable = false;         }     }      private void fillcameraactivities() {         getactivities();         if (!isactivityavailable) {             return;         }         cameraintents = new arraylist<intent>();         (resolveinfo resolveinfo : cameralist) {             intent intent = new intent(imagecaptureintent);             intent.setpackage(resolveinfo.activityinfo.packagename);             intent.setcomponent(new componentname(                     resolveinfo.activityinfo.packagename,                     resolveinfo.activityinfo.name));             intent.putextra(mediastore.extra_output, outputfileuri);             cameraintents.add(intent);         }     }      private void fillgallaryintent() {         galleryintent = new intent();         galleryintent.settype("image/*");         galleryintent.setaction(intent.action_pick);     }      public void showview() {         fillcameraactivities();         fillgallaryintent();         // chooser of filesystem options.         final intent chooserintent = intent.createchooser(galleryintent,                 "select source");          // add camera options.         chooserintent.putextra(intent.extra_initial_intents,                 cameraintents.toarray(new parcelable[] {}));          ((activity) context).startactivityforresult(chooserintent,                 image_pic_code);     }      private bitmap getbitmapfromurl(string src) {          final bitmapfactory.options options = new bitmapfactory.options();         options.injustdecodebounds = true;         bitmapfactory.decodefile(src, options);          // calculate insamplesize         options.insamplesize = calculateinsamplesize(options, 192, 256);          // decode bitmap insamplesize set         options.injustdecodebounds = false;         return bitmapfactory.decodefile(src, options);      }      private int calculateinsamplesize(bitmapfactory.options options,                                       int reqwidth, int reqheight) {         // raw height , width of image         final int height = options.outheight;         final int width = options.outwidth;         int insamplesize = 1;          if (height > reqheight || width > reqwidth) {              final int halfheight = height / 2;             final int halfwidth = width / 2;              // calculate largest insamplesize value power of 2             // , keeps both             // height , width larger requested height , width.             while ((halfheight / insamplesize) > reqheight                     && (halfwidth / insamplesize) > reqwidth) {                 insamplesize *= 2;             }         }          return insamplesize;     }      public string getrealpathfromuri(context context, uri contenturi) {         cursor cursor = null;         try {             string[] proj = { mediastore.images.media.data };             cursor = context.getcontentresolver().query(contenturi, proj, null,                     null, null);             int column_index = cursor                     .getcolumnindexorthrow(mediastore.images.media.data);             cursor.movetofirst();             return cursor.getstring(column_index);         } {             if (cursor != null) {                 cursor.close();             }         }     }      public void onresult(int requestcode, int resultcode, intent data) {         if (resultcode == activity.result_ok) {             if (requestcode == image_pic_code) {                 if (android.os.build.version.sdk_int >= android.os.build.version_codes.ice_cream_sandwich) {                     log.v("", "ics");                 } else {                     log.v("", " not ics");                 }                 boolean iscamera;                 if (data == null) {                     iscamera = true;                 } else {                     final string action = data.getaction();                      if (action == null) {                         iscamera = false;                     } else {                         iscamera = action                                 .equals(mediastore.action_image_capture);                     }                     if (android.os.build.version.sdk_int >= android.os.build.version_codes.ice_cream_sandwich                             && action != null) {                         log.v("", "action = null");                         iscamera = true;                     } else {                         log.v("", "action not null");                     }                 }                 if (iscamera) {                     selectedimageuri = outputfileuri;                     onresultcameraok();                 } else {                     selectedimageuri = data == null ? null : data.getdata();                     onresultgalleryok();                 }             }         }          if (requestcode == crop_camera_request) {             if (resultcode == activity.result_ok) {                 resultoncropokofcamera(data);             } else if (resultcode == activity.result_canceled) {                 resultoncroppingcancel();             }         }          if (requestcode == crop_gallary_request) {             if (resultcode == activity.result_ok) {                 resultoncropokofgallary(data);             } else if (resultcode == activity.result_canceled) {                 resultoncroppingcancel();             }         }      }      private void docropping(int code) {         log.v("", this.cameraimagefilepath);         intent cropintent = new intent("com.android.camera.action.crop");         cropintent.setdataandtype(selectedimageuri, "image/*");         cropintent.putextra("crop", "true");         cropintent.putextra("aspectx", 1);         cropintent.putextra("aspecty", 1);         cropintent.putextra("outputx", 256);         cropintent.putextra("outputy", 256);         cropintent.putextra("return-data", true);         try {             ((activity) context).startactivityforresult(cropintent, code);         } catch (exception e) {          }     }      private void onresultcameraok() {         this.cameraimagefilepath = absolutecameraimagepath;         this.bitmap = getbitmapfromurl(cameraimagefilepath);         docropping(crop_camera_request);     }      private void onresultgalleryok() {         this.cameraimagefilepath = selectedimageuri.tostring();         this.bitmap = getbitmapfromurl(getrealpathfromuri(context,                 selectedimageuri));         docropping(crop_gallary_request);     }      private void resultoncropokofcamera(intent data) {         this.bitmap = data.getextras().getparcelable("data");         log.v("", "cameraimagefilepath on crop camera ok => "                 + cameraimagefilepath);         setimageprofile();     }      private void resultoncropokofgallary(intent data) {         bundle extras2 = data.getextras();         this.bitmap = extras2.getparcelable("data");         setimageprofile();     }      private void resultoncroppingcancel() {         log.v("", "do cropping cancel" + cameraimagefilepath);         setimageprofile();     }      private void setimageprofile() {         log.v("", "cameraimagepath = > " + cameraimagefilepath);         if (ivpicture != null) {             if (bitmap != null) {                 ivpicture.setimagebitmap(bitmap);                  string ivpicture =condettenthfragment.getstringimage(bitmap);                  log.d("byte code -", ivpicture);                  /*intent = new intent(context, imageupload.class);                  string getrec = ivpicture;                  //create bundle                 bundle bundle = new bundle();                  //add data bundle                 bundle.putstring("moin", getrec);                  //add bundle intent                 i.putextras(bundle);                  //fire second activity                 context.startactivity(i);*/              } else {                 log.v("", "bitmap null");             }         }     }      public string getvar1() {         return ivpicture1;     }      /*public string getstringimage(bitmap bmp){         bytearrayoutputstream baos = new bytearrayoutputstream();         bmp.compress(bitmap.compressformat.jpeg, 100, baos);         byte[] imagebytes = baos.tobytearray();         string encodedimage = base64.encodetostring(imagebytes, base64.default);         return encodedimage;     }*/ }   below fragment code:   public class condettenthfragment extends fragment {      static string filebyte;     string filename;     string resultlog;      imageview ivprofile;     context context = getactivity();     button btnupload, send;     camerahandler camerahandler;     private progressdialog pdialog;      static string abc;         @override        public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {            view rootview = inflater.inflate(r.layout.con_det_tenth_fragment, container, false);            /*textview tv = (textview) v.findviewbyid(r.id.tvfragfirst);            tv.settext(getarguments().getstring("msg"));*/             ivprofile = (imageview) rootview.findviewbyid(r.id.iv_upload);            btnupload = (button) rootview.findviewbyid(r.id.btn_upload_image);            send = (button) rootview.findviewbyid(r.id.btnsend);            camerahandler = new camerahandler(context);            camerahandler.setivpicture(ivprofile);             // progress dialog            pdialog = new progressdialog(getactivity());            pdialog.setcancelable(false);            pdialog.setmessage("please wait");              btnupload.setonclicklistener(new view.onclicklistener() {                @override                public void onclick(view v) {                     camerahandler.showview();                 }            });             send.setonclicklistener(new view.onclicklistener() {                @override                public void onclick(view v) {                    new async().execute();                }            });          return rootview;    }       public static condettenthfragment newinstance(string text) {          condettenthfragment f = new condettenthfragment();         bundle b = new bundle();         b.putstring("msg", text);          f.setarguments(b);          return f;     }       public static string getstringimage(bitmap bmp){         bytearrayoutputstream baos = new bytearrayoutputstream();         bmp.compress(bitmap.compressformat.jpeg, 100, baos);         byte[] imagebytes = baos.tobytearray();         string encodedimage = base64.encodetostring(imagebytes, base64.default);          abc = encodedimage;          //encodedimage = filebyte.settext().tostring();          return encodedimage;     }      // async task perform login operation     class async extends asynctask<string, void, string> {           @override         protected string doinbackground(string... params) {              //get bundle             /*bundle bundle = getintent().getextras();              //extract data…             string stuff = bundle.getstring("moin");*/              soapobject request = new soapobject(namespace, method_name);             request.addproperty(parameter, abc);//add parameters             request.addproperty(parameter2, "moin.jpeg");              soapserializationenvelope envelope = new soapserializationenvelope(soapenvelope.ver11);//set soap version             envelope.setoutputsoapobject(request);             envelope.dotnet = true;             try {                 httptransportse androidhttptransport = new httptransportse(url);                 // actual part call webservice                 androidhttptransport.call(soap_action, envelope);                 //  soapprimitive prim = (soapprimitive) envelope.getresponse();  // soapresult envelope body.                 soapobject response = (soapobject) envelope.bodyin;                 //  resultlog=prim.tostring();                  hidedialog();              } catch (exception e) {                 e.printstacktrace();             }              return resultlog;          }     }       /*@override     public void onclick(view view) {         if (view == btnupload) {             camerahandler.showview();         }         if (view == send) {             new async().execute();         }     }*/      @override     public void onactivityresult(int requestcode, int resultcode, intent data) {         camerahandler.onresult(requestcode, resultcode, data);         log.v("", "code = > " + requestcode);     }     // used show diologue     private void showdialog() {         if (!pdialog.isshowing())             pdialog.show();     }      // used hide diologue     private void hidedialog() {         if (pdialog.isshowing())             pdialog.dismiss();     }  } 


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -