android - Share image with ShareActionProvider from Picasso -


i spand few hours find solution...

so decided share informatiom, maybe 1 itwill helpful :)

the first way, shown below, takes bitmap view , loads file.

// access imageview  imageview ivimage = (imageview) findviewbyid(r.id.ivresult); // fire async request load image picasso.with(context).load(imageurl).into(ivimage); 

and later assuming after image has completed loading, how can trigger share:

// can triggered view event such button press public void onshareitem(view v) {     // access bitmap image view     imageview ivimage = (imageview) findviewbyid(r.id.ivresult);     // access uri bitmap     uri bmpuri = getlocalbitmapuri(ivimage);     if (bmpuri != null) {         // construct shareintent link image         intent shareintent = new intent();         shareintent.setaction(intent.action_send);         shareintent.putextra(intent.extra_stream, bmpuri);         shareintent.settype("image/*");         // launch sharing dialog image         startactivity(intent.createchooser(shareintent, "share image"));         } else {         // ...sharing failed, handle error     } }  // returns uri path bitmap displayed in specified imageview public uri getlocalbitmapuri(imageview imageview) {     // extract bitmap imageview drawable     drawable drawable = imageview.getdrawable();     bitmap bmp = null;     if (drawable instanceof bitmapdrawable){        bmp = ((bitmapdrawable) imageview.getdrawable()).getbitmap();     } else {        return null;     }     // store image default external storage directory     uri bmpuri = null;     try {         file file =  new file(environment.getexternalstoragepublicdirectory(               environment.directory_downloads), "share_image_" + system.currenttimemillis() + ".png");         file.getparentfile().mkdirs();         fileoutputstream out = new fileoutputstream(file);         bmp.compress(bitmap.compressformat.png, 90, out);         out.close();         bmpuri = uri.fromfile(file);     } catch (ioexception e) {         e.printstacktrace();     }     return bmpuri; } 

make sure add appropriate permissions androidmanifest.xml:

<uses-permission android:name="android.permission.write_external_storage"/> <uses-permission android:name="android.permission.read_external_storage" /> 

the second way share image not require write image file. code can safely executed on ui thread. approach suggested on webpage http://www.nurne.com/2012/07/android-how-to-attach-image-file-from.html .

imageview siv = (imageview) findviewbyid(r.id.ivresult); drawable mdrawable = siv.getdrawable(); bitmap mbitmap = ((bitmapdrawable)mdrawable).getbitmap();  string path = images.media.insertimage(getcontentresolver(),      mbitmap, "image description", null);  uri uri = uri.parse(path); return uri; 

you drawable imageview. bitmap drawable. put bitmap media image store. gives path can used instead of file path or url. note original webpage had additional problem immutable bitmaps, solved drawing bitmap canvas (never shown on screen). see linked page above details.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -