开发者

Write to SDCard file fails without exception

I have gone through multiple threads about SDcard file writing problems, but could not see a answer that would help me.

  1. I have used
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

in my manifest for node.

2. Following code creates a file but doesn't write to it. Writer never throws an exception either. When I go back to SDCard and see the file created is of 0 kb. ( which is due to createnew function)

3. SDCard shows the file created with these permissions ----rwxr-x ( write not being there for others )

4. The behaviour is same on emulator or devices liek ASUS pad or Acer Iconia devices.

开发者_JAVA技巧What is the regular way applications create files with content on SDCard from an Android application ?

if(isExternalStorageWritable()) {
    File testFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),Filename);
    try {
        if (testFile.createNewFile()) {
            Log.i(TAG + ": createSdcardFile","Empty file on external storage created");
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
                   openFileOutput(Filename, MODE_APPEND)));
            out.write(Content);
            out.close();
            Log.i(TAG + ": createSdcardFile","Content to new file created");
            created = true;
        }
    } catch(IOException e) {
        Log.e(TAG,"Unable to create/write to new file on external storage. Terminating testCDMReadPositive");
        Log.e(TAG,e.getMessage());
    }
}

4.Using mode MODE_WORLD_WRITABLE gives same result i.e. a 0kb file on SDCard

5. Anyone with experience in writing files on externalstorage ( /mnt/sdcard ) would know how to do this. Any help or direction, highly appreciated. Thank you


Maybe a stupid suggestion, but did you delete the old testfile before the new run? Because your if clause will prevent writing to the file if it already existed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜