android taking the screenshot and sharing it -


hey trying add share button app takes screenshot , share ist via facebook etc. had search net stackoverflow there many threads related issue couldnt figure out yet. confusing me .. in every example images filepath hardcoded did not useful , dynamic , trying take screenshot of moment , share it.. when give filepath myself takes picture folder , share that

public void clickbutton(view v) {     intent shareintent = new intent(android.content.intent.action_send);      //set type       shareintent.settype("image/png");      //add subject       shareintent.putextra(android.content.intent.extra_subject,             "car example");      //build body of message shared       string sharemessage = "an app...";      //add message       shareintent.putextra(android.content.intent.extra_text, sharemessage);      //add img     shareintent.putextra(intent.extra_stream,             uri.parse("/storage/sdcard0/tutorial_screenshot/screenshot0.jpg"));      //start chooser sharing       startactivity(intent.createchooser(shareintent, "share")); } 

as can see int add image part give filepath myself. how give more dynamic behaviour .. mean when click button app's screen has saved in folder , can share without hardcoding filepath.. great

edited after trying solution below;

well thanks. i've called shareit() right under onclick of button , application stops.. here log.

12-28 15:53:01.660: e/androidruntime(14120): fatal exception: main 12-28 15:53:01.660: e/androidruntime(14120): process: com.hede.namesurfer, pid: 14120    12-28 15:53:01.660: e/androidruntime(14120): java.lang.nullpointerexception 12-28 15:53:01.660: e/androidruntime(14120):    @       com.hede.namesurfer.mainactivity.share(mainactivity.java:161) 12-28 15:53:01.660: e/androidruntime(14120):    @     com.hede.namesurfer.mainactivity$1.onclick(mainactivity.java:42) 12-28 15:53:01.660: e/androidruntime(14120):    @  android.view.view.performclick(view.java:4438) 12-28 15:53:01.660: e/androidruntime(14120):    @  android.view.view$performclick.run(view.java:18422) 12-28 15:53:01.660: e/androidruntime(14120):    @  android.os.handler.handlecallback(handler.java:733)  12-28 15:53:01.660: e/androidruntime(14120):    @   android.os.handler.dispatchmessage(handler.java:95) 12-28 15:53:01.660: e/androidruntime(14120):    @  android.os.looper.loop(looper.java:136) 12-28 15:53:01.660: e/androidruntime(14120):    @  android.app.activitythread.main(activitythread.java:5017) 12-28 15:53:01.660: e/androidruntime(14120):    @  java.lang.reflect.method.invokenative(native method) 12-28 15:53:01.660: e/androidruntime(14120):    @ java.lang.reflect.method.invoke(method.java:515) 12-28 15:53:01.660: e/androidruntime(14120):    @  com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 12-28 15:53:01.660: e/androidruntime(14120):    @     com.android.internal.os.zygoteinit.main(zygoteinit.java:595)  12-28 15:53:01.660: e/androidruntime(14120):    @     dalvik.system.nativestart.main(native method) 12-28 15:53:02.780: i/process(14120): sending signal. pid 

try this

public void shareit()     {         view view =  findviewbyid(r.id.layout);//your layout id         view.getrootview();         string state = environment.getexternalstoragestate();         if (environment.media_mounted.equals(state))          {             file picdir  = new file(environment.getexternalstoragedirectory()+ "/mypic");             if (!picdir.exists())             {                 picdir.mkdir();             }             view.setdrawingcacheenabled(true);             view.builddrawingcache(true);             bitmap bitmap = view.getdrawingcache(); //          date date = new date();             string filename = "mylove" + ".jpg";             file picfile = new file(picdir + "/" + filename);             try              {                 picfile.createnewfile();                 fileoutputstream picout = new fileoutputstream(picfile);                 bitmap = bitmap.createbitmap(bitmap, 0, 0, bitmap.getwidth(), (int)(bitmap.getheight()/1.2));                 boolean saved = bitmap.compress(compressformat.jpeg, 100, picout);                 if (saved)                  {                     toast.maketext(getapplicationcontext(), "image saved device pictures "+ "directory!", toast.length_short).show();                 } else                  {                     //error                 }                 picout.close();             }              catch (exception e)              {                 e.printstacktrace();             }             view.destroydrawingcache();         } else {             //error          }             intent sharingintent = new intent(android.content.intent.action_send);         sharingintent.settype("image/jpeg");         sharingintent.putextra(intent.extra_stream, uri.parse(picfile.getabsolutepath()));         startactivity(intent.createchooser(sharingintent, "share via"));     } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -