File access from native code

7,125 views
Skip to first unread message

sofarsogood

unread,
Apr 18, 2011, 11:48:55 AM4/18/11
to android-ndk
hi

I need to create/access files residing in my application's folder in
internal memory(data/data/com.myapp.myappname) from native code.
fopen() fails with permission denied error.

I am able to access/create files in the sd card, but it fails when I
write to internal memory. Can anybody suggest a solution?

Thx

Ryan Routon

unread,
Apr 18, 2011, 1:27:45 PM4/18/11
to android-ndk
Basically you have to pipe a file descriptor and offset from java to
your c code.

It was discussed in length here:
http://groups.google.com/group/android-ndk/browse_thread/thread/4e25a5dfd46f8fea

Dianne Hackborn

unread,
Apr 18, 2011, 2:23:53 PM4/18/11
to andro...@googlegroups.com
First don't use raw paths.  You should use JNI to call Context.getFilesDir() to get the directory at which to put your files.

Given you have the correct path, you can just use normal C APIs to read and write files there; nothing else special is needed.


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

sofarsogood

unread,
Apr 18, 2011, 5:48:56 PM4/18/11
to android-ndk
So, would passing the correct dir from JNI be enough?

Or, would I need to open the file in JNI and pass fd as suggested by
Ryan?

On Apr 18, 11:23 am, Dianne Hackborn <hack...@android.com> wrote:
> First don't use raw paths.  You should use JNI to call Context.getFilesDir()
> to get the directory at which to put your files.
>
> Given you have the correct path, you can just use normal C APIs to read and
> write files there; nothing else special is needed.
>
>
>
>
>
>
>
>
>
> On Mon, Apr 18, 2011 at 8:48 AM, sofarsogood <anuragbha...@gmail.com> wrote:
> > hi
>
> > I need to create/access files residing in my application's folder in
> > internal memory(data/data/com.myapp.myappname) from native code.
> > fopen() fails with permission denied error.
>
> > I am able to access/create files in the sd card, but it fails when I
> > write to internal memory. Can anybody suggest a solution?
>
> > Thx
>
> > --
> > 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
> Android framework engineer
> hack...@android.com

sofarsogood

unread,
Apr 18, 2011, 6:04:30 PM4/18/11
to android-ndk
More details -

These files will be created by the native code. So, passing an fd from
the JNI layer would be a difficult implementation for me. There is no
discussion on file creation from native environment in the link
(thread) posted by Ryan.

Dianne Hackborn

unread,
Apr 18, 2011, 6:08:53 PM4/18/11
to andro...@googlegroups.com
You just need to have the files directory.  You can freely create files within that directory.
hac...@android.com

sofarsogood

unread,
Apr 19, 2011, 1:16:49 PM4/19/11
to android-ndk
Thx. I'll try and let you know.

sofarsogood

unread,
Apr 20, 2011, 3:54:53 PM4/20/11
to android-ndk
It worked. Thx.

Is there a way to get this path from within the native code? Its
difficult for me to pass the path down the layers to the native
component that creates the files.

Dianne Hackborn

unread,
Apr 20, 2011, 7:52:54 PM4/20/11
to andro...@googlegroups.com
Unfortunately you need to write the JNI code to do so.
hac...@android.com

sofarsogood

unread,
Apr 20, 2011, 8:33:01 PM4/20/11
to android-ndk
Okay.

Thx for your help.

On Apr 20, 4:52 pm, Dianne Hackborn <hack...@android.com> wrote:
> Unfortunately you need to write the JNI code to do so.
>

Chris Stratton

unread,
Apr 28, 2011, 2:33:17 AM4/28/11
to android-ndk
On Apr 20, 3:54 pm, sofarsogood <anuragbha...@gmail.com> wrote:

> Is there a way to get this path from within the native code? Its
> difficult for me to pass the path down the layers to the native
> component that creates the files.

On startup you could discover the path in java and jni call a simple
function which copies it into a globally accessible native buffer for
use by later native functions.
Reply all
Reply to author
Forward
0 new messages