Can I access the files I create with USB
I'm lost here.
I create files using this (stripped) code :开发者_C百科
File dir = getBaseContext().getDir(dirPath, MODE_WORLD_WRITEABLE);
try {
File file = new File(dir, fileName);
FileOutputStream fous = new FileOutputStream(file);
fous.write(data);
fous.flush();
fous.close();
long l = file.length();
Log.i("PpCameraActivity", "File size : " + l);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "Error while trying to write photo file", Toast.LENGTH_LONG).show();
}
I can verify with logcat that my file seems to be created (it has a not null lenght). But I cannot see it when I connect my android device to my PC. So... where is my file ? Is it hidden ? Erased ?
EDIT : I'm now trying to write on the SDCard specifically, using this :
File root = Environment.getExternalStorageDirectory();
File jpegFile = new File(root.getAbsolutePath() + "/myApplication/" + filePath);
try {
jpegFile.mkdirs();
FileOutputStream fous = new FileOutputStream(jpegFile);
fous.write(data);
fous.flush();
fous.close();
Log.i("PpCameraActivity", "File written : " + jpegFile.getAbsolutePath());
Toast.makeText(getBaseContext(), "File written : " + jpegFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
long l = jpegFile.length();
Log.i("PpCameraActivity", "File size : " + l);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "Error while trying to write photo file", Toast.LENGTH_LONG).show();
}
But I get a FileNotFoundException on the FileOutputStream creation...
OK found it.
Not an Android problem but just my error (not the first time) : mkdirs must be applied to the parent file, not the file I want to write...
So, for people interested :
Access the sd card using
File root = Environment.getExternalStorageDirectory();
Don't forget to require this permission
WRITE_EXTERNAL_STORAGE
Then make, as usual, mkdirs and file creation.
And don't forget : the android device cannot write on the sdard while it is mounted on you PC.
You probably aren't writing to the SD card, and the SD contents are all you can see from a USB connection.
Try something like this: http://androidgps.blogspot.com/2008/09/writing-to-sd-card-in-android.html (just the first thing that came up when I searched for "Android write to SD card").
精彩评论