• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How to save data in external storage in csv file

kotzen8989

Lurker
Jan 13, 2021
1
0
Hello. I`m really apologize for maybe very simple questions for android studio. Actually I`m beginner in android studio, before that made just project like "Hello World". So, decide to make 3 objects for text view , 3 objects for edit text and just one button [![][1]][1]for saving entered characters in csv file. Searched explanation in internet but had just sample for TxT files. So I changed program code from this sample,
and finally it saving in csv file, but I think it recognized yet as txt file, as solid text without 3 objects(text view) and also it just rewriting data instead previous file. I will send you the sauce and image for csv file (desired result) below. Please help me to decide this problem.

(1)activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:eek:rientation="vertical">

<TextView
android:id="@+id/View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="Name"
android:textSize="24sp"
android:textStyle="bold"/>



<EditText
android:id="@+id/editcsv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@String/edit_csv"
android:inputType="text" />
<TextView
android:id="@+id/View1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="home address"
android:textSize="24sp"
android:textStyle="bold"/>

<EditText
android:id="@+id/editcsv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@String/edit_csv1"
android:inputType="text" />
<TextView
android:id="@+id/View2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="telephone number"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="@+id/editcsv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@String/edit_csv2"
android:inputType="text" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:eek:rientation="horizontal">

<Button
android:id="@+id/saveBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@String/save_csv" />


</LinearLayout>

</LinearLayout>

(2)strings.xml:

<resources>
<string name="app_name">ExternalStorageApp</string>
<string name="save_csv">save</string>
<string name="edit_csv">Please enter your name</string>
<string name="edit_csv1">Please enter your home address</string>
<string name="edit_csv2">Please enter your telephone number</string>
<string name="View">Name</string>
<string name="View1">home address</string>
<string name="View2">telephone number</string>
</resources>

(3)MainActivity.java

import android.os.Bundle;
import android.os.Environment;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.app.ListActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private EditText editText, editText1, editText2;
private Button saveBtn;




private String fileName = "mFile.csv";
private String filePath = "MyFileStorage";

private File mFile;
private String mData = "";




@override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initilization
editText = (EditText) findViewById(R.id.editcsv);


saveBtn = (Button) findViewById(R.id.saveBtn);
saveBtn.setOnClickListener(this);



// check external storage
// and aviability for saving
if (!isAvailable() || isReadOnly()) {
// if unavailable set button as enabled
// for saving and reading

saveBtn.setEnabled(false);

} else {
// if available get files for ExternalStorage
mFile = new File(getExternalFilesDir(filePath), fileName);

}

}

// check external storage for reading
private static boolean isReadOnly() {
String storageState = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED_READ_ONLY.equals(storageState);
}

// check availability for external storage
private static boolean isAvailable() {
String storageState = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(storageState);
}

// simple method for pop up windows
public void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}

@override
public void onClick(View v) {

// entered datas for mData
mData = "" ;

try {

FileOutputStream fos = new FileOutputStream(mFile);

fos.write(editText.getText().toString().getBytes());
fos.write(editText1.getText().toString().getBytes());
fos.write(editText2.getText().toString().getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
showToast("Files was saved");

}
}
 

Attachments

  • csv file.jpg
    csv file.jpg
    176.4 KB · Views: 219
  • csv file (changed).jpg
    csv file (changed).jpg
    179.4 KB · Views: 178

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones