Save bitmap to android default pictures directory -


i use below code save bitmap image (captured layout) android default picture directory. seems saved image corrupt because gallery can not open this.

when save bitmap in location gallery can open it. not opening when save android default directory.

   public void savetogallery() {         string path = environment.getexternalstoragedirectory().tostring()                 + "/pictures/keshavarzi/" + "screenshot-" + system.currenttimemillis() + ".png";          viewgroup v = (viewgroup) findviewbyid(r.id.lyt_main_report_activity);         v.setdrawingcacheenabled(true);         v.setdrawingcacheenabled(true);         v.builddrawingcache();         bitmap bitmap = bitmap.createbitmap(v.getdrawingcache());         v.setdrawingcacheenabled(false);            outputstream out = null;         file imagefile = new file(path);          try {             out = new fileoutputstream(imagefile);             bitmap.compress(bitmap.compressformat.jpeg, 90, out);             out.flush();         } catch (filenotfoundexception e) {             e.printstacktrace();         } catch (ioexception e) {             e.printstacktrace();         } {             try {                 if (out != null) {                     out.close();                 }              } catch (exception exc) {             }          }           contentvalues values = new contentvalues();         values.put(mediastore.images.media.title, "title");         values.put(mediastore.images.media.description, "description");         values.put(mediastore.images.media.date_taken, system.currenttimemillis());         values.put(mediastore.images.media.mime_type, "image/jpeg");         values.put(mediastore.mediacolumns.data, path);          getcontentresolver().insert(mediastore.images.media.external_content_uri, values);           mhtoast.showtoast(getstring(r.string.saved_in_gallery), toast.length_long);     } 

try this, check if save directory exists or create directory first save bitmap,

string path = environment.getexternalstoragedirectory().tostring()             + "/pictures/keshavarzi/" + "screenshot-" +   system.currenttimemillis() + ".png"; file imagefile = new file(path); if(!imagefile.getparentfile().exists()){       imagefile.getparentfile().mkdirs(); } 

Comments

Popular posts from this blog

java - BeanIO write annotated class to fixedlength -

Using Java 8 lambdas/transformations to combine and flatten two Maps -

How to connect android app to App engine -