This is my first post here, and i should advise that my knowledge is at best limited!
I am trying to create an app for my android phone.
I need to eventually output values input by the user to a file, most probably a txt file, that can be retreived direct from the phone when connected via a usb cable.
However, I am struggling to understand how to achieve this.
From what I understand that I need to ensure the card is mounted,
To get the path,
and then use FileOpenStream(), with FileWriter
to ‘load’ strings into the file (sorry not sure on the proper way to desribe that, or anythign else for that matter)
I have also allowed Permission for in the manifest to write to the sdcard. However, all the exmaples i can find seem to not relate to the sdcard. any advise, and or pointing to tutorials gratefully recieved.
my current code, is all over the place as i have confused myself to much, sorry.
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
if (mExternalStorageAvailable == true &&
mExternalStorageAvailable == true) {
//Environment.getExternalStorageDirectory() gets root path to SD card
// FileOutputStream to output somehow
File path = Environment.getExternalStorageDirectory();
OutputStream out = null;
try {
//File path = Environment.getExternalStorageDirectory();
//FileOutputStream f = new FileOutputStream(new File(path + "/DCIM", "fileName"));
out = new BufferedOutputStream(new FileOutputStream(file));
OutputStreamWriter osw = new OutputStreamWriter(f);
osw.write("test txt");
osw.flush();// ensure that everything is really written out and close
osw.close();
Toast.makeText(getBaseContext(), "path = " + path, Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getBaseContext(), "File not FOund", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getBaseContext(), "IOException", Toast.LENGTH_LONG).show();
}
}
EDIT:
Here is the XML File (hopefully), to see if the FileNotFound exception is be caused by it.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidbook.epcsn"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<permission></permission>
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true" android:permission="android.permission.WRITE_EXTERNAL_STORAGE">
I cleaned up your code a bit and changed a few lines and I think it does what you want now. I used a BufferedWriter to write to the sdcard.
You can read more about the BufferedWriter I used here