从ArrayList Android生成CSV

时间:2017-10-07 21:51:57

标签: android csv arraylist opencsv

我正在创建一个应用程序,在onPreviewFrame上进行一些图像处理,然后生成每个帧的Y,U和V值。然后我将这些值存储到ArrayList中。

示例来自日志(Y,U,V)

10-08 05:06:10.941 20405-20405/redlight55.com.bpreader D/MyEntryData: [1590690, 2700692, 5731888]
10-08 05:06:10.998 20405-20405/redlight55.com.bpreader D/MyEntryData: [1610252, 2699200, 5760320]
10-08 05:06:11.014 20405-20405/redlight55.com.bpreader D/MyEntryData: [1612862, 2700420, 5763556]
10-08 05:06:11.089 20405-20405/redlight55.com.bpreader D/MyEntryData: [1617378, 2699508, 5770000]

我想将这些数据存储到CSV文件中,该文件将保存在设备的内部存储器中。 它们应该存储为像这样的东西

enter image description here

这是我保存并生成ArrayList

日志的方法
@Override
public void onPreviewFrame(int ySum, int uSum, int vSum) {
    img_Y_Avg = ySum;
    img_U_Avg = uSum;
    img_V_Avg = vSum;

    //set value of Y on the text view
    TextView valueOfY = (TextView)findViewById(R.id.valueY);
    valueY = img_Y_Avg;
    valueOfY.setText(Double.toString(img_Y_Avg));

    //set value of U on the text view
    TextView valueOfU = (TextView)findViewById(R.id.valueU);
    valueU = img_U_Avg;
    valueOfU.setText(Double.toString(img_U_Avg));

    //set value of V on the text view
    TextView valueOfV = (TextView)findViewById(R.id.valueV);
    valueV = img_V_Avg;
    valueOfV.setText(Double.toString(img_V_Avg));

    //store value to array list
    ArrayList<Integer> yAverage = new ArrayList<Integer>();
    yAverage.add(img_Y_Avg);
    //Log.d("MyEntryData", String.valueOf(yAverage));

    //store u values to array
    ArrayList<Integer> uAverage = new ArrayList<Integer>();
    uAverage.add(img_U_Avg);
    //Log.d("MyEntryData", String.valueOf(uAverage));

    //store u values to array
    ArrayList<Integer> vAverage = new ArrayList<Integer>();
    vAverage.add(img_V_Avg);
    //Log.d("MyEntryData", String.valueOf(vAverage));


    ArrayList<Integer> getValues = new ArrayList<Integer>();
    for(int i = 0; i < uAverage.size(); i++) {
        getValues.add(yAverage.get(i));
        getValues.add(uAverage.get(i));
        getValues.add(vAverage.get(i));
    }

    Log.d("MyEntryData", String.valueOf(getValues));


    handler = new Handler();
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            readingRemaining = readingRemaining -1;

            if (readingRemaining > 0){
                plotGraph(img_Y_Avg);
            }
        }
    };

    handler.postDelayed(runnable, 100);

    //Log.d("MyEntryData", String.valueOf(img_Y_Avg +" "+ img_U_Avg+" "+img_V_Avg));
}

我一直在尝试使用opencsv,但没有文件生成到设备存储中。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

CSV是逗号分隔值文件,所以只需编写它。

String filename = "csvfile.csv";
FileOutputStream outputStream;

try {
  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  for(int i = 0; i < uAverage.size(); i+=3) {
      outputStream.write((getValues.get(i)+",").getBytes());
      outputStream.write((getValues.get(i+1)+",").getBytes());
      outputStream.write((getValues.get(i+2)+"\n").getBytes());
  }
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

答案 1 :(得分:1)

String filename = "csvfile.csv";

    File directoryDownload = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    File logDir = new File (directoryDownload, "bpReader"); //Creates a new folder in DOWNLOAD directory
    logDir.mkdirs();
    File file = new File(logDir, filename);

    FileOutputStream outputStream = null;
    try {
           outputStream = new FileOutputStream(file, true);
           //outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
           for (int i = 0; i < uAverage.size(); i += 3) {
                outputStream.write((getValues.get(i) + ",").getBytes());
                outputStream.write((getValues.get(i + 1) + ",").getBytes());
                outputStream.write((getValues.get(i + 2) + "\n").getBytes());

            }
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

似乎这是正确的答案。 Shmuel发布的答案有助于创建文件并将列分割为我想要的。但是,我必须将文件目录声明到外部存储器中。

问题是,我有一部只有内部存储的手机。所以我认为我需要做些什么来将它存储到内部存储中。我可能错了,但事实证明,如果手机只有内部存储空间,它实际上就是外部存储设备。

我可能完全错了,但对我来说就是这样。