我使用以下代码创建了一个CSV文件:
package com.tukajo.ioiograph;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.jjoe64.graphview.CustomLabelFormatter;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.LineGraphView;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;
import au.com.bytecode.opencsv.CSVWriter;
public class graph_fragment extends Fragment {
Boolean csvThreadRunning;
private customIOIO _myIOIO;
globalControlInstance globalData = globalControlInstance.getInstance();
private final Handler mHandler = new Handler();
CSVWriter writer = null;
private Runnable mTimer1;
private Runnable mTimer2;
private GraphView graphView;
private int i = 0;
ToggleButton realTimeToggle;
TextView graphPeakText;
TextView graphAvgText;
TextView graphSamplesText;
ToggleButton csvToggler;
private double avgSumPin39 = 0.0;
private double avgSumPin40 = 0.0;
private double avgSumPin41 = 0.0;
private double avgSumPin42 = 0.0;
private double avgSumPin43 = 0.0;
int graphLoopCounter = 0;
private double newValuePin39 = 0;
private double newValuePin40 = 0;
private double newValuePin41 = 0;
private double newValuePin42 = 0;
private double newValuePin43 = 0;
public static Fragment newInstance() {
Fragment fragment = new graph_fragment();
return fragment;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
_myIOIO = (customIOIO) getActivity().getApplicationContext();
_myIOIO.create(getActivity().getBaseContext(), getActivity());
// _myIOIO = MainActivity.get_myIOIO();
_myIOIO.start();
graphPeakText = (TextView) rootView.findViewById(R.id.graphpeak);
graphAvgText = (TextView) rootView.findViewById(R.id.graphaverage);
realTimeToggle = (ToggleButton) rootView
.findViewById(R.id.realtimetoggle);
realTimeToggle
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
globalData.setLiveUpdatingEnabled(isChecked);
if (isChecked) {
updateGraphUI();
Log.v("PRESSED", "Button Pressed");
} else {
pauseGraphUI();
globalData.setGraphPeak1(0.0);
globalData.setGraphPeak2(0.0);
globalData.setGraphPeak3(0.0);
globalData.setGraphPeak4(0.0);
globalData.setGraphPeak5(0.0);
}
}
});
// set the initial text values for the graph peaks and the graph avgs
graphPeakText.setText("Graph Peaks: " + globalData.getGraphPeak1()
+ " " + globalData.getGraphPeak2() + " "
+ globalData.getGraphPeak3() + " " + globalData.getGraphPeak4()
+ " " + globalData.getGraphPeak5());
graphAvgText.setText("NOT IMPLEMENTED");
GraphView graphView = new LineGraphView(getActivity(),
"Graph with 5 lines");
graphView.getGraphViewStyle().setNumHorizontalLabels(5);
graphView.getGraphViewStyle().setNumVerticalLabels(8);
graphView.addSeries(globalData.getGraphViewSeries1());
graphView.addSeries(globalData.getGraphViewSeries2());
graphView.addSeries(globalData.getGraphViewSeries3());
graphView.addSeries(globalData.getGraphViewSeries4());
graphView.addSeries(globalData.getGraphViewSeries5());
graphView.setViewPort(0, 300);
graphView.setScrollable(true);
LinearLayout layoutForGraph = (LinearLayout) rootView
.findViewById(R.id.graphLayout);
layoutForGraph.addView(graphView);
csvToggler = (ToggleButton) rootView.findViewById(R.id.ToggleButton2);
csvToggler
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
try {
csvThreadRunning = true;
exportToCSV();
} catch (IOException e) {
e.printStackTrace();
}
} else {
csvThreadRunning = false;
}
}
});
Log.i("Path of File = ", Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ File.separator
+ "csvData"
+ File.separator + "csvDataFile.txt");
return rootView;
}
public void exportToCSV() throws IOException {
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ File.separator
+ "csvData"
+ File.separator + "csvDataFile.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
writer = new CSVWriter(new FileWriter(file), ',');
new Thread() {
public void run() {
if (csvThreadRunning) {
while (!Thread.currentThread().isInterrupted()) {
try {
String s1 = String.valueOf(globalData
.getLatestGraphData1()) + "pin39"; // array
// of
// your
// values
writer.writeNext(s1);
s1 = String.valueOf(globalData
.getLatestGraphData2()) + "pin40";
writer.writeNext(s1);
s1 = String.valueOf(globalData
.getLatestGraphData3()) + "pin41";
writer.writeNext(s1);
s1 = String.valueOf(globalData
.getLatestGraphData4()) + "pin42";
writer.writeNext(s1);
s1 = String.valueOf(globalData
.getLatestGraphData5()) + "pin43";
writer.writeNext(s1);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
};
}
public void pauseGraphUI() {
mHandler.removeCallbacks(mTimer1);
mHandler.removeCallbacks(mTimer2);
}
public void updateGraphUI() {
mTimer2 = new Runnable() {
@Override
public void run() {
double newReadingDoublePin39 = 0.0;
double newReadingDoublePin40 = 0.0;
double newReadingDoublePin41 = 0.0;
double newReadingDoublePin42 = 0.0;
double newReadingDoublePin43 = 0.0;
newReadingDoublePin39 = _myIOIO.getCurrentPin39Voltage();
newReadingDoublePin40 = _myIOIO.getCurrentPin40Voltage();
newReadingDoublePin41 = _myIOIO.getCurrentPin41Voltage();
newReadingDoublePin42 = _myIOIO.getCurrentPin42Voltage();
newReadingDoublePin43 = _myIOIO.getCurrentPin43Voltage();
graphLoopCounter++;
if (globalData.isLiveUpdatingEnabled()) {
globalData.setGraph2LastXValue(globalData
.getGraph2LastXValue() + 1d);
globalData.getGraphViewSeries1().appendData(
new TukajoData(globalData.getGraph2LastXValue(),
newReadingDoublePin39), true, 1000);
globalData.getGraphViewSeries2().appendData(
new TukajoData(globalData.getGraph2LastXValue(),
newReadingDoublePin40), true, 1000);
globalData.getGraphViewSeries3().appendData(
new TukajoData(globalData.getGraph2LastXValue(),
newReadingDoublePin41), true, 1000);
globalData.getGraphViewSeries4().appendData(
new TukajoData(globalData.getGraph2LastXValue(),
newReadingDoublePin42), true, 1000);
globalData.getGraphViewSeries5().appendData(
new TukajoData(globalData.getGraph2LastXValue(),
newReadingDoublePin43), true, 1000);
}
// avgSum += newReadingDoublePin39 + newReadingDoublePin40 +
// newReadingDoublePin41 + newReadingDoublePin42 +
// newReadingDoublePin43;
mHandler.postDelayed(this, 100);
Log.v("UPDATING", "Updating the graph");
Log.v("newReadingValue", String.valueOf(newReadingDoublePin39));
}
};
mHandler.postDelayed(mTimer2, 100);
}
}
然而,当我查看DDMS视图时,我看不到文件在任何地方被创建,任何想法为什么没有出现?我没有从logcat收到任何错误。
包含文件预期位置的屏幕截图:
答案 0 :(得分:1)
使用日志检查此文件的确切路径。我认为您正在错误的位置寻找文件。
使用:
Log.I("PAth of File = ", Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ File.separator
+ "csvData"
+ File.separator + "csvDataFile.txt");
在代码中使用上面的log语句并检查文件的完整路径并验证该路径中的文件。