How to Access the SD File System?

2,142 views
Skip to first unread message

James SDC

unread,
Sep 1, 2009, 1:48:51 PM9/1/09
to android-ndk
Hi,

I am trying to use JNI (through NDK) to open a file and read/write to
the file. Does anyone know how to do that, and is the root access
necessary?

In addition, can a Java Android application access the file in a SD
card without root access necessary?

Thanks.

PK

unread,
Sep 2, 2009, 4:24:23 AM9/2/09
to android-ndk
Don't know how to access SD card using JNI, but have java app example
how to list files in the directory:

public String[] getFilesList(){
String fileList[] = null;

String sdcardState = android.os.Environment.getExternalStorageState();
if (sdcardState.contentEquals(android.os.Environment.MEDIA_MOUNTED)){
dirName = android.os.Environment.getExternalStorageDirectory() +
File.separator + "some_directory";
File dir = new File(dirName);
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File file,String name){
return (name.endsWith(".png") || name.endsWith(".jpg"));
}
};

fileList = dir.list(filter);
return fileList;
}
return null;
}

to read files use standart Java java.io.Reader or java.io.InputStream
subclasses.

WoodManEXP

unread,
Sep 2, 2009, 10:43:20 AM9/2/09
to android-ndk
I am working on the exact same thing right now (using JNI through NDK
to access files)!

I have found that the native java.io read methods are are giving good
performance when pulling in large binary byte[] arrays from /sdcard/.
Problem is that is the only thing they can read directly from the file
system. If you need to convert that binary data to another type like
char or short or int you are SOL because you either code bit shifting
tricks to do it yourself or use java.nio classes. Both methods are
slower than slow with large arrays.

To your question, it was easy to access the /sdcard/. Just append "/
sdcard/" to the beginnng of the file name. No special permssions got
in my way. (Need to configure an /sdcard/ for the avd if you have not
done so already).

Have you been successful installing and configuring NDK?

James SDC

unread,
Sep 2, 2009, 1:32:57 PM9/2/09
to android-ndk
Hi PK,

Thank you very much for your information. I have figured out the file
access to the SD card in Java. Basically, it is working without root
access necessary.

Hi WoodManEXP,

Are you using Java or JNI to access the file on SD card? It sounds to
me that you are using Java. The problem I have is to use JNI to access
the file.

First of all, I try to use fopen() to open an existing file on SD
card, then I want to use fread() to read the file, and use fwrite() to
write to the file.

Let say I call fopen("/sdcard/mytest.dat", "r+") to open the existing
file. On HTC Hero, I got errno = 13, which is permission denied. Then
I change the phone image and grant the root access according to some
sayings on the Internet. After that, the fopen() returns me success,
but fread() returns me errno = 2, which is "no such file or
directory."

The error of fread() totally does not make sense to me, and I wonder
if it is caused by the phone image change.

WoodManEXP, what is your experience with this, and how do you exactly
do to access the file from JNI? Thank you in advance!

James SDC

unread,
Sep 2, 2009, 2:22:24 PM9/2/09
to android-ndk
Hi WoodManEXP,

I may want to try on the Emulator as well. So how to configuration
NDK, and how to configuration ADV?

Thank you very much!
James.

Dianne Hackborn

unread,
Sep 2, 2009, 2:58:52 PM9/2/09
to andro...@googlegroups.com
Native code and Java have the same permissions.  After all, your Java call is just boiling down to a native open call.
--
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.

WoodManEXP

unread,
Sep 2, 2009, 3:41:17 PM9/2/09
to android-ndk
Hi James,

You are right, I am accessing the /sdcard/ via Java and my plan is to
begin accessing through JNI. I have seen posts that say it may be
possible to pass an open FileInputStream to the JNI and get the fd
from it. But there are fre more steps remaining to get the NDK going
before I get to that part. Sorry to not be of more help at this point.

BTW, are you on Windows and if so what are you using to edit the C
code files?

Thank you,

WoodManEXP

unread,
Sep 2, 2009, 3:47:54 PM9/2/09
to android-ndk
I am still working through the NDK configuration and so don't know
enough yet to help. Configuring the /sdcard/ for the emulator is very
easy. Use the tools/android command to make an avd, maybe like this

android create avd -n my_avd -t 2 -c 256M

This will make an 1.5 SDK emulator with a 256M /sdcard/.

See http://developer.android.com/guide/developing/tools/avd.html

Hope that is what you wer asking.

James SDC

unread,
Sep 2, 2009, 4:46:02 PM9/2/09
to android-ndk
Hi WoodManEXP,

I usually use Visual Studio to edit the c code. Sometimes, I use Vim
to make it simpler.
I have added a SD card to my emulator now, and see how it is going on
emulator.

Hi Dianne,

You mentioned that Java and JNI have the same permissions. However,
why can my Java application be able to open/read/write to the file on
SD card, but the fopen("/sdcard/mytest.dat", "r+") in JNI gets
"permission denied"?

Can you point out how to open/read/write the file on SD card, by using
JNI?

Thanks,
James.

Dianne Hackborn

unread,
Sep 2, 2009, 5:57:49 PM9/2/09
to andro...@googlegroups.com
On Wed, Sep 2, 2009 at 1:46 PM, James SDC <hanz...@gmail.com> wrote:
You mentioned that Java and JNI have the same permissions. However,
why can my Java application be able to open/read/write to the file on
SD card, but the fopen("/sdcard/mytest.dat", "r+")  in JNI gets
"permission denied"?

I don't know, but all the Java code does is open the file, I can assure you.  You could try using the lower-level open() call.  And if you look at the filesystem, you will see that all of the files on the SD card are world readable.

Mine

unread,
Sep 2, 2009, 9:26:46 PM9/2/09
to andro...@googlegroups.com
Hi James,

    I'm using Emulator to do the same thing as you did, which is using fopen to open a file, and fread to read the file. Everything works fine.
    The difference is that I only need to read the file, so the fopen parameter is "rb". So I suggest that you try read the file first by fopen with "rb", then try write to see if anything different.

BRs,
Lei Yu

James SDC

unread,
Sep 3, 2009, 8:43:44 PM9/3/09
to android-ndk
Hi Lei Yu,

Yes, it is working now to me. I have tried both fopen() and open(),
and they all are working to me on both the emulator and the phone.
Thanks for all of you to help me out.

Best Regards,
James.

Sureshbabu

unread,
Oct 15, 2009, 2:31:28 AM10/15/09
to android-ndk
Hello All,

I am new to this android platform. Can i get some information on
developing JNI code (file read/write)?

I am using windows OS for development (eclipse).

How to develope JNI and how to use it in android java application?

Anyone can help on this.

In advance Thanks Much.

-tsbabu.

Jack Palevich

unread,
Oct 15, 2009, 9:14:41 AM10/15/09
to andro...@googlegroups.com
Sorry that everything is so confusing. Windows development is unfortunately different than Android development. (Android development is pretty similar to Unix/Linux development.)

Some suggestions:

+ Learn the standard Linux routines for file i/o. Do a web search for "fopen" or "stdio".

+ Learn how JNI works.  The Android implementation of JNI is very similar to the standard Java implementation, so you can use the documentation for the standard version to learn how to use the Android version.

+ Learn how the NDK works. There are documents and sample code in the NDK.

Jack Palevich

unread,
Oct 15, 2009, 9:17:35 AM10/15/09
to andro...@googlegroups.com
D'Oh, I think I should have said:

+ Learn how the Android permission system works. Starting with the Android 1.6 SDK, your apk has to have this permission in its manifest in order to be able to write to the SD card:

uses-permission 
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

Sureshbabu

unread,
Oct 21, 2009, 1:45:24 AM10/21/09
to android-ndk
Hi Jack,

Thank you very much for your quick response.

I am developing android application in windows platform with Eclipse
editor.
Android-ndk available for windows platform. So can we develop the jni
code in windows platform (eclipse editor)?

-tsbabu.

On Oct 15, 6:17 pm, Jack Palevich <jack...@google.com> wrote:
> D'Oh, I think I should have said:
> + Learn how the Android permission system works. Starting with the Android
> 1.6 SDK, your apk has to have this permission in its manifest in order to be
> able to write to the SD card:
>
> uses-permission
> android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
>
>
>
> On Thu, Oct 15, 2009 at 6:14 AM, Jack Palevich <jack...@google.com> wrote:
> > Sorry that everything is so confusing. Windows development is unfortunately
> > different than Android development. (Android development is pretty similar
> > to Unix/Linux development.)
> > Some suggestions:
>
> > + Learn the standard Linux routines for file i/o. Do a web search for
> > "fopen" or "stdio".
>
> > + Learn how JNI works.  The Android implementation of JNI is very similar
> > to the standard Java implementation, so you can use the documentation for
> > the standard version to learn how to use the Android version.
>
> > + Learn how the NDK works. There are documents and sample code in the NDK.
>
> > On Wed, Oct 14, 2009 at 11:31 PM, Sureshbabu <sureshbabu.th...@gmail.com>wrote:
>
> >> Hello All,
>
> >> I am new to this android platform. Can i get some information on
> >> developing JNI code (file read/write)?
>
> >> I am using windows OS for development (eclipse).
>
> >> How to develope JNI and how to use it in android java application?
>
> >> Anyone can help on this.
>
> >> In advance Thanks Much.
>
> >> -tsbabu.
>
> >> On Sep 4, 5:43 am, James SDC <hanzon...@gmail.com> wrote:
> >> > Hi Lei Yu,
>
> >> > Yes, it is working now to me. I have tried both fopen() and open(),
> >> > and they all are working to me on both the emulator and the phone.
> >> > Thanks for all of you to help me out.
>
> >> > Best Regards,
> >> > James.- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages