2017-10-07 93 views
0

我正在創建一個應用程序,在onPreviewFrame上進行一些圖像處理,然後生成每幀的Y,U和V值。然後我將這些值存儲到ArrayList中。從日誌從ArrayList生成CSV Android

例如(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做,但沒有文件生成到設備存儲中。我怎樣才能做到這一點?

回答

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(); 
} 
+0

仍然存在相同的問題。在內部存儲器中找不到該文件。一旦所有數據都從預覽框中生成,那麼文件將被存儲,因此我添加了if語句來檢查相機是否爲空。我仍然沒有在內部存儲器中獲得瓷磚。 – Mill3r

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發佈的答案有助於創建文件並將列分成我想要的。但是,我必須將文件目錄聲明爲外部存儲。

問題是,我有一個只有內部存儲的電話。所以我認爲我需要做一些事情來存儲它到內部存儲。我可能會錯,但事實證明,如果手機只有內部存儲器,它實際上是作爲外部存儲器。

我對此可能完全錯誤,但對我來說就是它的樣子。

+1

添加清單的WRITE_EXTERNAL_STORAGE權限。 使用Environment.getExternalStorageState()獲取文件夾 – Shmuel