Fairly new to android, I come from a heavy fortran background. I've been trying to make apps, sucessful until now.
I'm having trouble finding a way for: saving an 'edittext' field by use of a button(save), then saving this user inputted data to a .csv file(preferably in internal storage).
I've found many articles but everyone glazes over the fundemental part I want(above).
the best idea I've got, is of generating the .csv in the class, then creating a method to save the 'edittext' as a new string, then to output that string to the .csv
Hopefully this can be simply explained, I just cant find this simple explanation anywhere, or at-least that I can understand...
Please try this.I hope this code helps you.
CSVFileWriter.java
public class CSVFileWriter {
private PrintWriter csvWriter;
private File file;
public CSVFileWriter(File file) {
this.file = file;
}
public void writeHeader(String data) {
try {
if (data != null) {
csvWriter = new PrintWriter(new FileWriter(file, true));
csvWriter.print(",");
csvWriter.print(data);
csvWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
SampleActivity.java
public class SampleActivity extends Activity {
CSVFileWriter csv;
StringBuffer filePath;
File file;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
saveButton = (Button) findViewById(R.id.button1);
editText = (EditText) findViewById(R.id.editText1);
filePath = new StringBuffer();
filePath.append("/sdcard/abc.csv");
file = new File(filePath.toString());
csv = new CSVFileWriter(file);
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
csv.writeHeader(editText.getText().toString());
}
});
}
}
Add this in manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Related
I am trying desperately to read a file from Android Studio asset folder. The file is "ok.txt" and contains the string: yo chicken mcgriddle fiddle fiddle.
Per this video: https://youtu.be/1CHDASXojNQ and stackoverflow browsing, this is the solution I came up with:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
tv_text = (TextView) findViewById(R.id.nameView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
tv_text.setText(LoadData("ok.txt"));
}
public String LoadData(String inFile) {
String tContents = "";
try {
InputStream stream = getAssets().open(inFile);
int size = stream.available();
byte[] buffer = new byte[size];
stream.read(buffer);
stream.close();
tContents = new String(buffer);
} catch (IOException e) {
// Handle exceptions here
}
return tContents;
}
});
}
}
I am inclined to believe that the code works, but, it does not return the String. In the app, it returns a blank message in the place of the textview's "hello world" placeholder after clicking the button. I thought it was due to the limitation of the textview so I modified the constraint size, but the blank persists. Anybody know what's up?
I have attempted to create an App that plays wavfiles that have been created from another app I have made that records wavFiles.
The wav files I would like to play are stored in the following directory with my android device.
"/data/data/com.example.androidaudiorecorder/files/"
Here is the code I have attempted so far :
public class MainActivity extends AppCompatActivity {
Button playEvent ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playEvent= (Button) findViewById(R.id.btnPlayEvent);
playEvent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playEvent( );
}
});
}
public static void playEvent(){
try {
MediaPlayer player = new MediaPlayer();
player = new MediaPlayer();
player.setDataSource("/data/data/com.example.androidaudiorecorder/files/recording_DOG.wav");
player.prepare();
player.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("Exception of type : " + e.toString());
e.printStackTrace();
}
}
}
}
Unfortunatley when I load the app and press play, nothing happens.
I am not sure if this is the correct way to use a file directory.
Any advice on how to fix this would be greatly appreciated.
1) You need to give permissions in the android manifest file to access storage before reading/writing any content int the storage .
2) To read the file,
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/filename.extension");
I'm creating an app to open a PDF application from a button, but what I did is not going well.
In my layout there is only one button , which seeks want and open a PDF file.
Not giving error in Eclipse , just in tablet appears " readerPDF stopped ."
I do not know what to do. somebody help me.
Sorry for my english.
public class ReaderActivity extends Activity {
private Button btnOpen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
btnOpen.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String filename = null;
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
}
}
});
}
}
filename is null, so the path you are passing to the third-party app does not point to a PDF file.
I'm new to java, but a quick learner.
I'm trying make my Android App to take user-input from an EditText, search an Excel column for a matching string, and return the value of (the same row of) another column.
I added a jxl.jar on the build path, and my code seems like it should work. Teaching myself, I am slowly figuring out the debugging. The problem seems to be source android.jar was not found (if i remember correctly). I located it, and now I get "the source attachment does not contain the source for the file instrumentation.class"
This is my first app using excel. Do I need to address the Android Manifest somehow?
Here's my code, just in case, and any help is very appreciated!:
public class Frame_Bore extends Activity {
private String inputFile;
public void setInputFile(String inputFile) {
this.inputFile = "c:/Desktop/AAS Work/PDA Programs/JBDB.xls";
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.framebore);
Button Btn1 = (Button) findViewById(R.id.button1);
final EditText T1 = (EditText) findViewById(R.id.et1);
final EditText T2 = (EditText) findViewById(R.id.et2);
Btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String t1Value = T1.getText().toString();
File inputWorkbook = new File(inputFile);
Workbook w;
String bore = null;
try {
w = Workbook.getWorkbook(inputWorkbook);
Sheet sheet = w.getSheet("Frame-Bore");
int y = 6;
while (y < 200) {
int x = 2;
Cell columnB = sheet.getCell(x, y);
String motorframe = columnB.getContents();
if (motorframe == t1Value) {
Cell columnC = sheet.getCell(x + 1, y);
bore = columnC.getContents();
} else {
y = y + 1;
}
}
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
T2.setText("" + bore);
}
}
});
}
You are trying to access c: drive ( "c:/Desktop/AAS Work/PDA Programs/JBDB.xls" ) from your emulator, which is not possible. You need to put your excel file either in the resources folder or in SD card, if you are running it in device.
An Emulator has a virtual SD Card, and its limitations is that it can not direct access any other drive from the computer. However you can do it by making a webservice which will perform operation on behalf of your android application. You need to call this web-service from your android application.
Put the file in assets folder and access it like this:
InputStream is = null;
try {
is = context.getAssets().open("JBDB.xls");
workbook = Workbook.getWorkbook(is);
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
if(workbook!=null)
sheet = workbook.getSheet("Frame-bore");
context is your main activity context.
My code seems to run but it does not display the result any result on text view am guessing is because of the way I set my code. The code is below Somebody please help me. Thanks
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Button button = (Button) findViewById(R.id.but);
input = (EditText) findViewById(R.id.editTextj);
display = (TextView) findViewById(R.id.textView8);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CCActivity3 fs = new CCActivity3();
fs.fileReader();
}// button
});// button end
}
public void fileReader() {
try {
InputStream is=this.getResources().openRawResource(R.raw.file);
BufferedReader bc = new BufferedReader(new InputStreamReader(is));
String cLine;
String inputText = "";
List<String> test2 = new ArrayList<String>();
// read file line by line
while ((cLine = bc.readLine()) != null) {
inputText = inputText + cLine + "\n";
}
s = input.getText().toString();
test = CCActivity3.getPermutation(s);//Permutation method
test2.retainAll(test);//intersection
String fg = "";
for (String s2 : test2) {
fg += s2 + "\n";
}
display.setText(fg);
bc.close();
} catch (Exception e) {// catch any errors if necessary
display.setText(e.getMessage());
}
}
If you check the resource line am very sure am not getting that right and also the formating of the code I believe they just scattered . Hint the file.txt on the res/raw path has more than 100,000 strings/words, could this be the cause.Thanks Again
Regarding your code:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CCActivity3 fs = new CCActivity3();
fs.fileReader();
}// button
});// button end
Is CCActivity3 derived from an Activity?
If so, then you shouldn't construct it this way.
You can't instantiate your own Activity class. Only the Android framework can do that.
What seems to be happening here is that inside fileReader(), you are calling getResources() on an Activity whose Context has not yet been properly initialized by onCreate().
In any case, one thing you can do is call fileReader() method directly without instantiating CCActivity3, like so:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
fileReader();
}// button
});// button end
test = CCActivity3.getPermutation(s);//Permutation method
test2.retainAll(test);//intersection
Above codes can get the right return?