星空网 > 软件开发 > 操作系统

Android文字图片写入CSV(Base64)并分享

  做的一个分享的功能,将文字图片以CSV的形式分享到邮件之类的应用。

  首先,CSV逗号分隔值文件格式(Comma-Separated Values),纯文本形式,逗号分隔,一行数据不跨行。

  图片转换成Base64字符串

public String writeBase64(String path) {          //path图片路径    byte[] data = null;    try {      InputStream in = new FileInputStream(path);      data = new byte[in.available()];      in.read(data);      in.close();    } catch (FileNotFoundException e) {      e.printStackTrace();    } catch (IOException e) {      e.printStackTrace();    }    return Base64.encodeToString(data, Base64.NO_WRAP);        //注意这里是Base64.NO_WRAP    }

最后返回 Base64.encodeToString(data, Base64.NO_WRAP),注意这里要使用Base64.NO_WRAP,而不是Base64.DEFAULT。default当字符串过长(RFC2045里规定了每行最多76个字符换行),自动会加入换行符,影响使用,用NO_WRAP解决。

  生成和写入CSV

public File writeCsv() {    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/csv/";      //CSV文件路径    File csv = new File(path + "KnowYourTeam.csv");    File pa = new File(path);    if (!pa.exists()) {      pa.mkdirs();    }    if (!csv.exists()) {      try {        csv.createNewFile();      } catch (IOException e) {        e.printStackTrace();      }    } else {      try {        csv.delete();                  //这里写的如果文件存在会删除文件新建一个文件        csv.createNewFile();      } catch (IOException e) {        e.printStackTrace();      }    }    try {      BufferedWriter bw = new BufferedWriter(new FileWriter(csv, true));      ArrayList<Person> list = new PersonDao(this).queryAll();                //数据库取Person List      for (Person person : list) {                                 //循环写入person数据(name,title,image)        String img = writeBase64(person.getPicPath());                    //getPicPath()路径        bw.write(person.getName() + "," + person.getTitle() + "," + img);        bw.newLine();                                       //换行,一行一组数据      }      bw.close();    } catch (IOException e) {      e.printStackTrace();    }    return csv;  }

  最后分享

          File csv = writeCsv();        Intent sendIntent = new Intent();        sendIntent.setAction(Intent.ACTION_SEND);        sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(csv));        sendIntent.putExtra(Intent.EXTRA_SUBJECT, "KnowYourTeam shared data");        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式        sendIntent.putExtra(Intent.EXTRA_TEXT, "There is CSV. " + df.format(new Date()));        sendIntent.setType("text/comma-separated-values");        startActivity(Intent.createChooser(sendIntent, "share"));

  Intent.ACTION_SEND  带附件发送

  Intent.createChooser(sentIntent, "share")  可以选择支持这种格式的应用打开分享

 




原标题:Android文字图片写入CSV(Base64)并分享

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

brickfox:https://www.ikjzd.com/w/1573153575396958209
铂赢奥(BringallPay):https://www.ikjzd.com/w/1573153577980305409
Broad Match:https://www.ikjzd.com/w/1573153585488453634
布朗诺圣诞乐园:https://www.ikjzd.com/w/1573153586495086593
Brookstone:https://www.ikjzd.com/w/1573153587010985985
Browntape:https://www.ikjzd.com/w/1573153587769810945
深圳有没有比较好玩的景点 深圳有没有比较好玩的景点推荐一下:https://www.vstour.cn/a/366175.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流