Android saves view as a picture and places it in an album

Source: Internet
Author: User

In Android, you can save a view as a picture and place it in a photo album, with View->bitmap->file, which converts the view to bitmap first, and then saves bitmap to the album.

  

You need to convert a red-framed view into a picture and keep it in the album. The XML code for view is

<relativelayout Android:id= "@+id/lanjing_code_rl"//view ID to saveAndroid:layout_width= "Match_parent"Android:layout_height= "0DP"Android:layout_margin= "20DP"Android:paddingbottom= "10DP"Android:layout_weight= "1"Android:background= "@drawable/round_main_bg" > <relativelayout Android:id= "@+id/lanjing_code_info"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparenttop= "true"Android:layout_marginleft= "20DP" > <ImageView Android:id= "@+id/self_head"Android:layout_width= "50DP"Android:layout_height= "50DP"Android:layout_alignparentleft= "true"Android:layout_alignparenttop= "true"Android:layout_margintop= "20DP"Android:background= "@drawable/defaultuser"/> <TextView Android:id= "@+id/self_name"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparenttop= "true"Android:layout_marginleft= "20DP"Android:layout_margintop= "20DP"Android:layout_torightof= "@+id/self_head"android:gravity= "Center"Android:maxlines= "2"Android:textcolor= "@color/article_black"android:textsize= "16SP"Android:textstyle= "Bold"/> <TextView Android:id= "@+id/self_hospital"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@+id/self_name"android:layout_gravity= "Center_horizontal"Android:layout_marginleft= "20DP"Android:layout_margintop= "3DP"Android:layout_torightof= "@+id/self_head"Android:textcolor= "@color/article_gray"android:textsize= "14sp"/> </RelativeLayout> <ImageView Android:id= "@+id/lanjing_code_img"Android:layout_width= "200DP"Android:layout_height= "200DP"Android:layout_below= "@+id/lanjing_code_info"Android:layout_centerhorizontal= "true"Android:layout_margintop= "10DP"Android:scaletype= "Fitcenter"/> <TextView Android:id= "@+id/lanjing_code_txt"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@+id/lanjing_code_img"Android:layout_centerhorizontal= "true"Android:layout_margintop= "5DP"android:gravity= "Center"Android:textcolor= "@color/number_blue"Android:textscalex= "1.5"android:textsize= "23sp"/> <TextView Android:id= "@+id/lanjing_tishi"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@+id/lanjing_code_txt"Android:layout_centerhorizontal= "true"Android:layout_margintop= "5DP"Android:text= "Sweep the QR code above, add me to the Blue whale Doctor"Android:textcolor= "@color/solid_gray"android:textsize= "14sp"/> </RelativeLayout>

Save Method

private void Saveerweima () {  
     File erweimaimg = fileutilss.geterweimaimgfile (Mylanjingcode.this);
if(Erweimaimg = =NULL) { return; } //Lanjing_code_rl.setbackgroundcolor (Getresources (). GetColor (R.color.solid_white)); if(false==lanjing_code_rl.isdrawingcacheenabled ()) {lanjing_code_rl.setdrawingcacheenabled (true); } lanjing_code_rl.builddrawingcache (); Bitmap Bitmap=Lanjing_code_rl.getdrawingcache ();
String path = Erweimaimg.getabsolutepath (); FileOutputStream
out
=NewFileOutputStream (path); Bmp.compress (Bitmap.CompressFormat.PNG,100, out); Out.flush ();     Out.close (); Lanjing_code_rl.setdrawingcacheenabled (false); Mediascannerconnection.scanfile (Mylanjingcode. This,Newstring[] {environment.getexternalstoragepublicdirectory (ENVIRONMENT.DIRECTORY_DCIM). GetPath ()+ "/" + path},NULL,NULL); Saveimgtogallery (path);
}
 Public Booleansaveimgtogallery (String fileName) {BooleanSdcardexist = Environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED);//determine if the SD card exists        if(!sdcardexist)return false; Try{contentvalues values=Newcontentvalues (); Values.put ("Datetaken",NewDate (). toString ()); Values.put ("Mime_type", "image/jpg"); Values.put ("_data", FileName); Application App=doctorapplication.getinstance (); Contentresolver CR=App.getcontentresolver ();        Cr.insert (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); } Catch(Exception e) {e.printstacktrace (); }        return true; }

In this way, you can see the pictures that remain in the album.

Android saves view as a picture and places it in an album

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.