0

I am creating an app where do some image processing on the onPreviewFrame and then generate the Y, U and V values of each frame. I then stored these values into an ArrayList.

example from the log (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]

I want to store these data into a CSV file which will be saved in the internal storage of the device. They should be stored as something like this

enter image description here

this is the method where i am saving and generating the log for the 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));
}

i have been trying to do it with opencsv but there are no files generated into the device storage. How can i do this?

2 Answers 2

1

CSV is Comma separated values file, so just write it.

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

still having the same problem. Cannot find the file inside the internal storage. The file will be stored once all the data are generated from the preview frame and therefore i added an if statement to check if the camera is null. still i do not get the tile inside the internal storage.
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();
        }

seems like this is the correct answer. The answers Shmuel posted helped to create the file and divide the column how i wanted. However, i have to declare file directory into external storage.

The problem was, I have a phone which have only internal storage. So i thought i need to do something to store it into the internal storage. I might me wrong, but it turns out if a phone is only having internal storage, it actually acts as the external storage.

I may be completely wrong on this but to me that is what it seemed like.

2 Comments

Add WRITE_EXTERNAL_STORAGE permission to manifest. Get the folder with Environment.getExternalStorageState()
The concept of storage on Android is different. When considering external storage we are not talking about removable storage, this is the biggest confusion. External storage refers to the accessible (internal) directory (\ storage), while internal storage is used only by the application to store its data. See more developer.android.com/training/data-storage?hl=en

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.