I am trying to get a file to save to my sdcard. Here is the code I have so far. I really appreciate any help you can offer. Thanks Edit: The problem is that the file is not saved. When I exit the program and look at the sdcard no file was saved. I put a toast into the error catchers and it looks like I'm getting an IOException. I don't know how to check the StackTrace log though
class bSaveListen implements Button.OnClickListener{
@Override
public void onClick(View arg0) {
savef();
}
}
public void savef(){
sdCard = Environment.getExternalStorageDirectory();
String filename = "aaaaaaa.txt";
file = new File(sdCard, filename);
FileOutputStream fileout;
byte[] thatString = new String("awesome string").getBytes();
try {
fileout = new FileOutputStream(file);
fileout.write(thatString);
fileout.flush();
fileout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Probably you are getting an exception, you simply are not seeing it. Instead of e.printStackTrace();
, which is useless since it doesn't show up in DDMS, use Le(TAG, "Error", e);
That way you should be able to see the Exception
and figure out what's wrong
I spent all day today trying to figure this out and I finally have an answer. Just thought I would post it here in case anyone else is having a similar issue. I stupidly wrote my permission inside the application block of code instead of below it. Should be like this
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Not like this
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</application>`
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.