做的一个分享的功能,将文字图片以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, "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")  可以选择支持这种格式的应用打开分享

更多相关文章

  1. Android(安卓)环境配置常见的两个错误
  2. android 报Unable to resolve target 'android-XX' 类似的错误。
  3. Android.mk for your own module
  4. Android(安卓)Studio上传SVN
  5. Android-Android(安卓)10 创建不了文件夹
  6. 手把手教你如何创建一个连接到Binder上的服务(图文)(一)
  7. 【Android笔记】执行命令行语句
  8. mac终端配置Android(安卓)ADB命令
  9. Android(安卓)Studio 快捷键整理分享

随机推荐

  1. 使用docker安装elasticsearch,head插件,在e
  2. 【http】还在用postman?curl它不香吗
  3. 【Java】有关强引用、软引用、弱引用、虚
  4. cookie在二级域名间共享完成sso
  5. 前技术总监优化tomcat的建议,派上用场了
  6. 【java】网络编程之BIO
  7. 如何解决netty自定义协议粘包分包问题
  8. 辣鸡,使用CAS机制完成SSO
  9. 刚升级没多久,fastjson又漏洞了,改换jackso
  10. MySQL的EXPLAIN其实很简单