I'm exploring saving internally versus saving on the sdcard. Currently, I'm trying to save a custom object (DummyTwice) on the sdcard, like so:
String root = (Environment.getExternalStorageDirectory().toString());
File dir = new File(root + PUB_EXT_DIR);
dir.mkdirs();
File file = new File(dir,FILE_NAME);
try {
DummyTwice dt = new DummyTwice(textEnter.getText().toString());
FileOutputStream os = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(dt);
oos.close();
os.close();
Toast.makeText(v.getContext(),"Object Saved",Toast.LENGTH_SHORT).show();
Toast.makeText(v.getContext(),file.getAbsolutePath(), Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Execution stops at:
FileOutputStream os = new FileOutputStream(file);
Catching the exception:
FileNotFoundException e
What am I doing wrong? Relevant constants:
private final static String PUB_EXT_DIR = /data
private final static String FILE_NAME = /obj.dat
you shouldn't write directly on the sdcard, and you should use
getExternalFilesDir(null)
instead of
Environment.getExternalStorageDirectory()
getExternalFilesDir(null)
returns a private path on the sdcard for your Activity
, like /sdcard/Android/data/package.your.app
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.