---
title: "Android文字图片写入CSV(Base64)并分享"
author: "bell0901"
date: "2015-06-09T14:11:13Z"
tags: ["Android", "csv"]
categories: ["移动开发"]
slug: "android-csv-base64"
draft: false
lastmod: "2015-06-09T14:19:55Z"
id: 2218080
url: "https://rewufu.iteye.com/blog/2218080"
---

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