Android text picture written to CSV (BASE64) and share

Source: Internet
Author: User

Do a share of the function, the text image in the form of CSV to share to the mail and other applications.

First, CSV comma-separated value file format (comma-separated values), plain text form, comma-delimited, row of data does not cross the line.

Picture converted to Base64 string

 Publicstring WriteBase64 (string path) {//path picture pathbyte[] data =NULL; Try{InputStream in=NewFileInputStream (path); Data=New byte[In.available ()];            In.read (data);        In.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }        returnbase64.encodetostring (data, base64.no_wrap); Note that this is base64.no_wrap}

Finally, return to base64.encodetostring (data, Base64.no_wrap), and note that Base64.no_wrap is used instead of base64.default. Default when the string is too long (RFC2045 the maximum of 76 characters per line), automatically add line breaks, affect use, with no_wrap resolution.

Generate and write CSV

 PublicFile writecsv () {String path= Environment.getexternalstoragedirectory (). GetAbsolutePath () + "/csv/"; CSV files path file csv=NewFile (path + "Knowyourteam.csv")); File PA=NewFile (path); if(!pa.exists ())        {Pa.mkdirs (); }        if(!csv.exists ()) {            Try{csv.createnewfile (); } Catch(IOException e) {e.printstacktrace (); }        } Else {            Try{csv.delete ();            Write here if the file exists will delete the file to create a new file Csv.createnewfile (); } Catch(IOException e) {e.printstacktrace (); }        }        Try{bufferedwriter bw=NewBufferedWriter (NewFileWriter (CSV,true)); ArrayList<Person> list =NewPersondao ( This). Queryall (); Database Fetch person List for(person person:list) {//cyclic write to Person data (name,title,image) String img /c8>=writeBase64 (Person.getpicpath ()); Getpicpath () path Bw.write (Person.getname ()+ "," + person.gettitle () + "," +img);                                       Bw.newline ();        Line break, one group of Data} bw.close (); } Catch(IOException e) {e.printstacktrace (); }        returncsv; }

Last Share

File csv =writecsv (); Intent sendintent=NewIntent ();                Sendintent.setaction (Intent.action_send);                Sendintent.putextra (Intent.extra_stream, Uri.fromfile (CSV)); Sendintent.putextra (Intent.extra_subject,"Knowyourteam Shared Data"); SimpleDateFormat DF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//Set Date formatSendintent.putextra (Intent.extra_text, "there is CSV." + Df.format (NewDate ())); Sendintent.settype ("Text/comma-separated-values"); StartActivity (Intent.createchooser (sendintent,"Share"));

Intent.action_send with Attachment send

Intent.createchooser (Sentintent, "share") can select apps that support this format to open sharing

Android text picture written to CSV (BASE64) and share

Related Article

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.