Internal Storage on HTC Desire
I'm trying to save a file to internal storage (not SD Card). As the documentation points out, if I use getExternalStorageDirectory()
, the data will be placed under /Android/data/<package_name>/
, but there's nothing there. This code is taken from here. Similar code worked on Archos, though.
File path = Environment.getExternalStorageDirectory();
File file = new File(path, "DemoPicture.jpg");
txtList.append(file.getPath());
try {
path.mkdirs();
InputStream is = getResources().openRawResource(R.drawable.icon);
OutputStream os = new FileOutputStream(file);
byte[开发者_StackOverflow中文版] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
} catch (IOException e) {
Log.d("ExternalStorage", "Error writing " + file, e);
}
I use Android SDK 2.1. Permission WRITE_EXTERNAL_STORAGE
is already added in the manifest. No error in logcat. Any hint?
You're confusing Internal and External storages. You are talking about internal storage, while actually you are using external storage:
File path = Environment.getExternalStorageDirectory();
The resulting file will be located here: /mnt/sdcard/DemoPicture.jpg
. But if your really want to use internal storage, take a look at Context.openFileOutput
function.
EDIT:
File path = Environment.getExternalStorageDirectory(); File file = new File(path, "DemoPicture.jpg"); txtList.append(file.getPath());
try {
path.mkdirs();
InputStream is = getResources().openRawResource(R.drawable.icon);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
} catch (IOException e) {
Toast.makeText(this, "Failed to write a file", Toast.LENGTH_LONG).show();
}
Forgot to update. The problem disappears when I simply unplug the device and run the app. A friend told me he got the same problem and solved it by closing any apps that are likely to access the SD card, e.g. Windows Explorer.
精彩评论