Access Android's SDCard by C++

3,679 views
Skip to first unread message

Android120

unread,
Mar 18, 2011, 6:14:44 AM3/18/11
to android-ndk
hi all.
I am trying program a application on Android by C++ and have problem
when attempt access to Android's SDCard. Have anyone know it, please
help me.

mic _

unread,
Mar 18, 2011, 12:15:47 PM3/18/11
to andro...@googlegroups.com
What kind of problem? You need to be more specific.
The SD card should be mounted as /sdcard or something similar. Do you have the correct permissions set for your app?

/Michael


--
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.


Dmitry Golubovsky

unread,
Mar 18, 2011, 12:52:19 PM3/18/11
to android-ndk
Hi,

Well, I had absolutely no problems doing that from C code by regular
open/read/write/close functions; C++ must be the same (e. g. this very
dumb logging function):

void jnilog(char *str) {
int fd = open("/system/media/sdcard/jni.log", O_RDWR | O_APPEND |
O_CREAT, 0666);
if(fd > -1) {
write(fd, str, strlen(str));
write(fd, "\n", 1);
close(fd);
}
}

Path to the file may be more configurable: in Java code you call
getExternalStorageDirectory() to obtain the SD card path that is known
to the system, and pass it to the native code. In fact /system/media/
sdcard will likely be the path to the "resident" storage device (at
least in my case, where files uploaded via USB end up, even when the
removable sdcard is not inserted).

Permission-wise, make sure you include in your AndroidManifest.xml

<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> somewhere
inside the <manifest> tag.

When installing your apk, you will see the warning that the
application wants to modify external storage; this means that your
request for permissions in the manifest xml was understood.

Also note that filesystem on the sdcard is most likely FAT32 (does not
distinguish between characters case in filenames).

Hope this helps.

MatthiasM

unread,
Mar 18, 2011, 12:56:48 PM3/18/11
to android-ndk
On Mar 18, 5:52 pm, Dmitry Golubovsky <golubov...@gmail.com> wrote:
> Path to the file may be more configurable: in Java code you call
> getExternalStorageDirectory() to obtain the SD card path that is known
> to the system, and pass it to the native code. In fact /system/media/
> sdcard will likely be the path to the "resident" storage device (at
> least in my case, where files uploaded via USB end up, even when the
> removable sdcard is not inserted).

I read the documentation up and down, but as far as I understand,
getExternalStorageDirectory() can *only* return the *internal* SD
card. I have not found a consistent way to find the path to the user's
supplied SD card. Howw can I reliably find that path?

- matthias

Dmitry Golubovsky

unread,
Mar 18, 2011, 1:57:10 PM3/18/11
to android-ndk
Matthias,

Here's what was discussed some time ago:

http://groups.google.com/group/android-developers/browse_thread/thread/2d472ec34e8935ed

I ran into the same issue and asked almost the same question, and as
Dianne explained close to the end of the thread there seems to be no
unification as to what to do if there are several storage units.

On Mar 18, 12:56 pm, MatthiasM <melcher.matth...@googlemail.com>
wrote:

Android120

unread,
Mar 21, 2011, 12:23:11 AM3/21/11
to android-ndk
Thanks Dmitry Golubovsky. it work very good.

Android120

unread,
Mar 21, 2011, 12:37:14 AM3/21/11
to android-ndk
Thanks Dmitry Golubovsky. it work very good.

On Mar 18, 9:52 am, Dmitry Golubovsky <golubov...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages