java - 在 DDMS View 中找不到 CSV 文件?

标签 java android file-io ddms opencsv

我有一个使用以下代码创建的 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 View 下查看时,我没有看到在任何地方创建的文件,有什么想法为什么没有出现吗?我没有从 logcat 收到任何错误。

包含文件预期位置的屏幕截图:

File Locator DDMS

最佳答案

使用 Log 检查该文件的确切路径。我认为您正在错误的位置查找文件。

用途:

     Log.I("PAth of File = ", Environment.getExternalStorageDirectory()
        .getAbsolutePath()
        + File.separator
        + "csvData"
        + File.separator + "csvDataFile.txt");

在代码中使用上述日志语句并检查文件的完整路径并验证该路径中的文件。

关于java - 在 DDMS View 中找不到 CSV 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25110269/

相关文章:

java - 通过鼠标中键捕获粘贴操作

java - Android 上的 getType

c++ - 如何使用 QByteArray 读取 16 位整数

windows - CreateFile 如何以 FILE_SHARE_READ 失败并以 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE 成功?

java - 从包外部使用包私有(private)构造函数访问最终静态类

java - 默认可变树节点图标

android - 如何在代码中打开或关闭 SwitchCompat

C 程序无法创建输出文本文件

java - 如何从 Java 程序内部将 powershell 脚本作为 Windows 服务运行?

android - LazyColumn 内的 LazyHorizo​​ntalGrid