[android-developers] Re: Impossible to Create & Write to or Read From Files in the SD Card
You're on the right track, but it's best not to hardcode the path to a file on the SD Card. Check out this API:
http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29
This will help you get the directory to the SD card. It usually isn't /sdcard, it's usually /mnt/sdcard, but it may move or may be renamed on devices, so it's best to use that or a similar API to help you find it. Check out these APIs also:
http://developer.android.com/reference/android/os/Environment.html#getDataDirectory%28%29
http://developer.android.com/reference/android/content/Context.html#getDir%28java.lang.String,%20int%29
http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir%28java.lang.String%29
http://developer.android.com/reference/android/content/Context.html#getFilesDir%28%29
There are a lot of functions in the API to help you find a good spot to put your data files depending on what you are doing. There are even more than this, but this should get you started.
Steven
Studio LFP
http://www.studio-lfp.com
On Sunday, October 23, 2011 6:08:22 PM UTC-5, melis wrote:
Hi all,--
i really tried hard but couldn't achieve to create or any other file
operations in Android. First thing i did was tp check the availabilty
of the SD card media, with the sample code here:
http://developer.android.com/guide/topics/data/data- .storage.html#filesExternal
I have the media and works properly.
Then i tried to create a .txt file with this code snippet:
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(txtData.getText());
myOutWriter.close();
fOut.close();
also tried this one, which works in the Java project but not in
android:
String path = "/sdcard/data.txt";
File dosya = new File(path);
dosya.createNewFile();
last thing i tried is another sample code from android developers;
private void createExternalStoragePrivateFile() {
// TODO Auto-generated method stub
File file = new File(getExternalFilesDir(null),"DemoFile.jpg");
InputStream is = getResources().openRawResource(R.drawable. icon);
try {
OutputStream os = new FileOutputStream(file);
byte[] data = new byte [is.available()];
is.read(data);
os.write(data);
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.w(" External Storage ", " Error writing " + file, e);
}
}
I'm getting no errors but also no files in the sd card. And i don't
forget to add <uses-permission
android:name="android.permission.WRITE_EXTERNAL_ STORAGE"/> permission
line into the manifest.
I'm checking the F driver after executing the program to see if any
txt file created, but nothing happened so far. I read SD card is not
available when the android device is hooked up to the computer via USB
in somewhere. To check this possibility, i also looked at the sd
card's context with an laptop sd card adapter but again, i have
nothing.
Please someone help me. I'm out of ideas.
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home