android - How to save a layout as a image in gallery? -


i want save linearlayout containing 2 imageview , there 2 different images in imageviews. want save layout in gallery.can please tell how may this

the code used is:

in xml:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical"      >      <linearlayout android:id="@+id/linear2"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:layout_weight="1"      >      <imageview         android:id="@+id/imageview21"         android:layout_width="fill_parent"     android:layout_height="fill_parent"         android:layout_weight="1"         android:src="@drawable/ic_launcher" />      <imageview         android:id="@+id/imageview22"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:layout_weight="1"         android:src="@drawable/ic_launcher" />      </linearlayout>      <relativelayout android:id="@+id/linear2"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:layout_weight="5"      >      <button         android:id="@+id/savelayout2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparenttop="true"         android:layout_centerhorizontal="true"         android:text="save" />      </relativelayout>  </linearlayout> 

in activity class...

    public class layoutdisplay2 extends activity {      button save;     linearlayout ll;     imageview iv1, iv2;     private static int result_load_image1 = 1;     private static int result_load_image2 = 2;      @override     protected void oncreate(bundle savedinstancestate) {         // todo auto-generated method stub         super.oncreate(savedinstancestate);          setcontentview(r.layout.layout2);         ll = (linearlayout) findviewbyid(r.id.linear2);         save = (button) findviewbyid(r.id.savelayout2);         save.setonclicklistener(new onclicklistener() {              @override             public void onclick(view arg0) {                 // todo auto-generated method stub                 ll.setdrawingcacheenabled(true);                 bitmap bitmap = ll.getdrawingcache();                  string root = environment.getexternalstoragedirectory()                         .tostring();                 file newdir = new file(root + "/saved_picture");                 newdir.mkdirs();                 random gen = new random();                 int n = 10000;                 n = gen.nextint(n);                 string fotoname = n + ".jpg";                 file file = new file(newdir, fotoname);                 string s = file.getabsolutepath();                 log.i("path of saved image.", s);                 system.err.print("path of saved image." + s);                  try {                     fileoutputstream out = new fileoutputstream(file);                     bitmap.compress(bitmap.compressformat.jpeg, 90, out);                     out.flush();                     toast.maketext(getapplicationcontext(), "photo saved",                             toast.length_short).show();                     out.close();                 } catch (exception e) {                     toast.maketext(getapplicationcontext(), "photo saved",                             toast.length_short).show();                     log.e("exception", e.tostring());                 }             }          });         iv1 = (imageview) findviewbyid(r.id.imageview21);         iv1.setonclicklistener(new onclicklistener() {              @override             public void onclick(view arg0) {                 // todo auto-generated method stub                 intent = new intent(                         intent.action_pick,                         android.provider.mediastore.images.media.external_content_uri);                  startactivityforresult(i, result_load_image1);             }         });         iv2 = (imageview) findviewbyid(r.id.imageview22);         iv2.setonclicklistener(new onclicklistener() {              @override             public void onclick(view arg0) {                 // todo auto-generated method stub                  intent in = new intent(                         intent.action_pick,                         android.provider.mediastore.images.media.external_content_uri);                  startactivityforresult(in, result_load_image2);             }         });      }      @override     protected void onactivityresult(int requestcode, int resultcode, intent data) {         // todo auto-generated method stub         super.onactivityresult(requestcode, resultcode, data);          if (requestcode == result_load_image1 && resultcode == result_ok                 && null != data) {             uri selectedimage = data.getdata();             string[] filepathcolumn = { mediastore.images.media.data };              cursor cursor = getcontentresolver().query(selectedimage,                     filepathcolumn, null, null, null);             cursor.movetofirst();              int columnindex = cursor.getcolumnindex(filepathcolumn[0]);             string picturepath = cursor.getstring(columnindex);             cursor.close();              try {                  iv1.setimagebitmap(bitmapfactory.decodefile(picturepath));             } catch (exception e) {                 e.printstacktrace();             }          }         if (requestcode == result_load_image2 && resultcode == result_ok                 && null != data) {             uri selectedimage = data.getdata();             string[] filepathcolumn = { mediastore.images.media.data };             toast.maketext(getapplicationcontext(), "in second",                     toast.length_short).show();             log.i("second", "in second");             cursor cursor = getcontentresolver().query(selectedimage,                     filepathcolumn, null, null, null);             cursor.movetofirst();              int columnindex = cursor.getcolumnindex(filepathcolumn[0]);             string picturepath = cursor.getstring(columnindex);             cursor.close();             try {                 iv2.setimagebitmap(bitmapfactory.decodefile(picturepath));             } catch (exception e) {                 e.printstacktrace();             }         }      }  } 

the exception getting is:

04-17 13:19:33.908: i/path of saved image.(30459): /mnt/sdcard/saved_picture/3400.jpg 04-17 13:19:33.918: e/exception(30459): java.io.filenotfoundexception: /mnt/sdcard/saved_picture/3400.jpg (no such file or directory) 

just add below permission in manifest file:

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

edit:

boolean issdpresent = android.os.environment.getexternalstoragestate().equals(android.os.environment.media_mounted);  if(issdpresent) {   // yes  } else {  // no } 

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -