Issues Accessing /sdcard/ using fopen.

2,572 views
Skip to first unread message

Gregory Ray

unread,
Mar 8, 2010, 6:31:09 AM3/8/10
to andro...@googlegroups.com
Hi Guys,

I am receiving a NULL file when using:
FILE* file = fopen((data_path + "/" + path).c_str(),"w+");

Where as data_path = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() passed in from JNI.
Where as path is arbitrary but the concatenated string = "/sdcard/test.mp3" (this is from my logs).

test.mp3 does not exist but my expectation is that it will be created with the w+ setting.

I created my AVD with an sdcard using:
./android create avd -n withsdcard -t 6 -c 25M

And also my android manifest has the following permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
(I verified this by looking at the application settings)

Another thing is that tmpfile() returns NULL as well, although I can see reasons why tmpfile may not work I would expect the fopen("/sdcard/foo.bar","w+") to work. Did I miss something?

I am also ok writing to anywhere in the filesystem but it is my understanding that this is the only place we can write files from the NDK, is this correct?

I feel that I have exhausted all known possibilities at the moment, please help.

--
Gregory Ray
COO, Seek Mobile Interactive, Inc.

---

This e-mail message, including any attachments, is for the sole use of the intended recipient(s) and may contain information that is confidential and protected by law from unauthorized disclosure. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply e-mail and destroy all copies of the original message.

Sean Hodges

unread,
Mar 8, 2010, 9:22:43 AM3/8/10
to andro...@googlegroups.com
I can't see anything wrong with your approach, though it's difficult
to tell without seeing how you wired up your JNI calls. Have you
checked whether there is a null character at the end of data_path?

Also thought I'd point you at the StackOverflow solution, in case you
haven't seen it:
http://stackoverflow.com/questions/1992953/file-operations-in-android-ndk

> --
> You received this message because you are subscribed to the Google Groups
> "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to
> android-ndk...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/android-ndk?hl=en.
>

fadden

unread,
Mar 8, 2010, 6:16:55 PM3/8/10
to android-ndk
On Mar 8, 3:31 am, Gregory Ray <gr...@seekmobileinteractive.com>
wrote:

> I am receiving a NULL file when using:
> FILE* file = fopen((data_path + "/" + path).c_str(),"w+");

Two thoughts:

(1) try plain fopen("/sdcard/test.mp3", "w+") to rule out any
weirdness with the string stuff
(2) print the return value from errno (LOGW("fopen(%s) failed: %s",
filename, strerror(errno));)

Hopefully these will provide a clue.

In the spirit of "is it plugged in", do an "echo blah > /sdcard/test ;
cat /sdcard/test" from the adb shell before running your test just to
make sure the SD card is mounted writable.

Gregory Ray

unread,
Mar 9, 2010, 10:43:22 PM3/9/10
to andro...@googlegroups.com
This is what I am seeing from the log:
failed: No such file or directory

Any ideas what might cause this?

Thanks,
Greg


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

Dianne Hackborn

unread,
Mar 9, 2010, 11:34:30 PM3/9/10
to andro...@googlegroups.com
Are you sure the SD card is mounted?  Have you tried creating that file with the shell?  Have you tried creating the file with the Java APIs?  There is absolutely no difference in functionality for accessing files via either java or the NDK (the java APIs are just wrappers for calling the native file functions), and apps can definitely write to the SD card if they have the permission, so it most likely it something generally wrong unrelated to the NDK.
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Gregory Ray

unread,
Mar 10, 2010, 12:11:07 AM3/10/10
to andro...@googlegroups.com
Dianne,

The shell revealed the problem to me once I saw the file was actually being created. Thanks for working late and the help.

-Greg
Reply all
Reply to author
Forward
0 new messages