java - Saving image to gallery from imageview in viewpager -


i managed save image imageview gallery onlongclicklistner() of code given below. problem save last image dosent matters image try save.

public class capturephotoutils {           public final string insertimage(contentresolver cr,                                                bitmap source,                                                string title,                                                string description) {              contentvalues values = new contentvalues();             values.put(mediastore.images.media.title, title);             values.put(mediastore.images.media.display_name, title);             values.put(mediastore.images.media.description, description);             values.put(mediastore.images.media.mime_type, "image/jpeg");             // add date meta data ensure image added @ front of gallery             values.put(mediastore.images.media.date_added, system.currenttimemillis());             values.put(mediastore.images.media.date_taken, system.currenttimemillis());              uri url = null;             string stringurl = null;    /* value returned */              try {                 url = cr.insert(mediastore.images.media.external_content_uri, values);                  if (source != null) {                     outputstream imageout = cr.openoutputstream(url);                     try {                         source.compress(bitmap.compressformat.jpeg, 50, imageout);                     } {                         imageout.close();                     }                      long id = contenturis.parseid(url);                     // wait until mini_kind thumbnail generated.                     bitmap minithumb = mediastore.images.thumbnails.getthumbnail(cr, id, mediastore.images.thumbnails.mini_kind, null);                     // backward compatibility.                     storethumbnail(cr, minithumb, id, 50f, 50f, mediastore.images.thumbnails.micro_kind);                 } else {                     cr.delete(url, null, null);                     url = null;                 }             } catch (exception e) {                 if (url != null) {                     cr.delete(url, null, null);                     url = null;                 }             }              if (url != null) {                 stringurl = url.tostring();             }              return stringurl;         }           private final bitmap storethumbnail(                 contentresolver cr,                 bitmap source,                 long id,                 float width,                 float height,                 int kind) {              // create matrix scale             matrix matrix = new matrix();              float scalex = width / source.getwidth();             float scaley = height / source.getheight();              matrix.setscale(scalex, scaley);              bitmap thumb = bitmap.createbitmap(source, 0, 0,                     source.getwidth(),                     source.getheight(), matrix,                     true             );              contentvalues values = new contentvalues(4);             values.put(mediastore.images.thumbnails.kind,kind);             values.put(mediastore.images.thumbnails.image_id,(int)id);             values.put(mediastore.images.thumbnails.height,thumb.getheight());             values.put(mediastore.images.thumbnails.width,thumb.getwidth());              uri url = cr.insert(mediastore.images.thumbnails.external_content_uri, values);              try {                 outputstream thumbout = cr.openoutputstream(url);                 thumb.compress(bitmap.compressformat.jpeg, 100, thumbout);                 thumbout.close();                 return thumb;             } catch (filenotfoundexception ex) {                 return null;             } catch (ioexception ex) {                 return null;             }         }     } 

i putting images viewpager getting images array of drawables

class custompageradapter extends pageradapter {

    context mcontext;     layoutinflater mlayoutinflater;      public custompageradapter(context context) {         mcontext = context;         mlayoutinflater = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service);     }      @override     public int getcount() {         return mresources.length;     }       @override     public boolean isviewfromobject(view view, object object) {         return view == ((linearlayout) object);     }      @override     public object instantiateitem(viewgroup container, int position) {         view itemview = mlayoutinflater.inflate(r.layout.image_slider_item, container, false);          imageview = (touchimageview) itemview.findviewbyid(r.id.imageview);         imageview.setimageresource(mresources[position]);          imageview.setonlongclicklistener(new view.onlongclicklistener() {             @override             public boolean onlongclick(view view) {                  capturephotoutils photoutils = new capturephotoutils();                 imageview.setdrawingcacheenabled(true);                 bitmap b = imageview.getdrawingcache();                 photoutils.insertimage(full_screen_slider.this.getcontentresolver(),                     b, "1image", "this downloaded image sample");                 toast.maketext(mcontext, "longpress ", toast.length_short).show();                  return true;             }         });          container.addview(itemview);          return itemview;     }      @override     public void destroyitem(viewgroup container, int position, object object) {         container.removeview((linearlayout) object);     }  } 

replace this

@override     public object instantiateitem(viewgroup container,final int position) {        final view itemview = mlayoutinflater.inflate(r.layout.image_slider_item, container, false);        final touchimageview imageview = (touchimageview) itemview.findviewbyid(r.id.imageview);         imageview.setimageresource(mresources[position]);          imageview.setonlongclicklistener(new view.onlongclicklistener() {             @override             public boolean onlongclick(view view) {                  capturephotoutils photoutils = new capturephotoutils();                 imageview.setdrawingcacheenabled(true);                 bitmap b = imageview.getdrawingcache();                 photoutils.insertimage(full_screen_slider.this.getcontentresolver(),                     b, "1image", "this downloaded image sample");                 toast.maketext(mcontext, "longpress ", toast.length_short).show();                  return true;             }         });          container.addview(itemview);          return itemview;     } 

it replacing view each time calls instantiateitem make final help.


Comments