Reading files on android

It seems that on android SDL wants to read files from the assets directory. I would like to read from an arbitrary location (really the external storage /sdcard). Is there some way for SDL to support this directly? My current thought is to implement my own SDL_RWOps that use plain posix file IO and pass those ops to SDL_LoadBMP() and whatnot.

I want to read files from the sdcard because I expect users to add additional data to my game (extra characters, stages, music, etc).

A follow up to this: I forced SDL to use standard stdio operations.

SDL/src/file/SDL_rwops.c:448

Code:

#if defined(ANDROID) && defined(DONT_CARE)

So instead of using the android functions it will fall to the #if defined(HAVE_STDIO_H) and use that instead. Now my application can load files from regular filesystem paths.

Hi, I’m struggling to get basic file IO working on android, using
SDL_RWops to decompress files from the .apk. Am I missing something
really obvious?

Here’s the code:
SDL_RWops *file_in = SDL_RWFromFile(filename, “rb”);
sprintf(buf, “%s/%s”, SDL_AndroidGetInternalStoragePath(),filename);
FILE *_file_out = fopen(buf, “wb”);
SDL_RWops *file_out = SDL_RWFromFP(_file_out, SDL_TRUE);
char buffer[CPBUFSIZE];
int size = SDL_RWread(file_in, buffer, 1, CPBUFSIZE);
while (size>0)
{
SDL_RWwrite(file_out, buffer, 1, size);
size = SDL_RWread(file_in, buffer, 1, CPBUFSIZE);
}
SDL_RWclose(file_out);
SDL_RWclose(file_in);

I figured it’d be easier to extract everything to internal storage so
I could leave the rest of the code the same as on other platforms.
But now I’ve tried also just loading them straight in with RWops and
there’s definitely a problem in that part.

Thanks,
M

I’m pretty much doing the same thing. I’m copying an asset from the apk to
internal storage so that it can be modified by the user (custom game
levels).

The only difference I can see is that my code uses SDL_RWFromFile() for
file_out instead of going through a FILE pointer. What problems are you
experiencing? Does logcat tell you anything interesting? Did you do a
sanity check over your paths and the function results? Are you using the
official SDL 2.0 build or are you using the latest from hg?

Jonny DOn Thu, Sep 19, 2013 at 10:03 PM, Michael Brough wrote:

Hi, I’m struggling to get basic file IO working on android, using
SDL_RWops to decompress files from the .apk. Am I missing something
really obvious?

Here’s the code:
SDL_RWops *file_in = SDL_RWFromFile(filename, “rb”);
sprintf(buf, “%s/%s”, SDL_AndroidGetInternalStoragePath(),filename);
FILE *_file_out = fopen(buf, “wb”);
SDL_RWops *file_out = SDL_RWFromFP(_file_out, SDL_TRUE);
char buffer[CPBUFSIZE];
int size = SDL_RWread(file_in, buffer, 1, CPBUFSIZE);
while (size>0)
{
SDL_RWwrite(file_out, buffer, 1, size);
size = SDL_RWread(file_in, buffer, 1, CPBUFSIZE);
}
SDL_RWclose(file_out);
SDL_RWclose(file_in);

I figured it’d be easier to extract everything to internal storage so
I could leave the rest of the code the same as on other platforms.
But now I’ve tried also just loading them straight in with RWops and
there’s definitely a problem in that part.

Thanks,
M


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

logcat doesn’t say anything relevant.

SDL_RWFromFile returns something non-NULL, but then trying to
SDL_RWread on that gives nothing / 0 bytes.

I got it from hg since the 2.0 release, not sure how to tell whether
it’s the stable/dev branch.

thanks,> ------------------------------

Message: 2
Date: Thu, 19 Sep 2013 23:20:08 -0400
From: Jonathan Dearborn
To: SDL Development List
Subject: Re: [SDL] reading files on android
Message-ID:
<CA+DSiHb1DO–AgBzQ1NC1gV27dtKsYy=1Q13fm-f6Xafyu5sTw at mail.gmail.com>
Content-Type: text/plain; charset=“iso-8859-1”

I’m pretty much doing the same thing. I’m copying an asset from the apk to
internal storage so that it can be modified by the user (custom game
levels).

The only difference I can see is that my code uses SDL_RWFromFile() for
file_out instead of going through a FILE pointer. What problems are you
experiencing? Does logcat tell you anything interesting? Did you do a
sanity check over your paths and the function results? Are you using the
official SDL 2.0 build or are you using the latest from hg?

Jonny D

On Thu, Sep 19, 2013 at 10:03 PM, Michael Brough <@Michael_Brough> wrote:

Hi, I’m struggling to get basic file IO working on android, using
SDL_RWops to decompress files from the .apk. Am I missing something
really obvious?

Here’s the code:
SDL_RWops *file_in = SDL_RWFromFile(filename, “rb”);
sprintf(buf, “%s/%s”, SDL_AndroidGetInternalStoragePath(),filename);
FILE *_file_out = fopen(buf, “wb”);
SDL_RWops *file_out = SDL_RWFromFP(_file_out, SDL_TRUE);
char buffer[CPBUFSIZE];
int size = SDL_RWread(file_in, buffer, 1, CPBUFSIZE);
while (size>0)
{
SDL_RWwrite(file_out, buffer, 1, size);
size = SDL_RWread(file_in, buffer, 1, CPBUFSIZE);
}
SDL_RWclose(file_out);
SDL_RWclose(file_in);

I figured it’d be easier to extract everything to internal storage so
I could leave the rest of the code the same as on other platforms.
But now I’ve tried also just loading them straight in with RWops and
there’s definitely a problem in that part.

Thanks,
M


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Any chance that SDL_GetError() tells you anything? Have you checked your
read buffer data to see which operation (read or write) is having problems?
If reading is a problem, can you successfully load assets via SDL_image,
SDL_mixer, or something else that uses SDL_RWops?

Jonny DOn Fri, Sep 20, 2013 at 1:35 PM, Michael Brough wrote:

logcat doesn’t say anything relevant.

SDL_RWFromFile returns something non-NULL, but then trying to
SDL_RWread on that gives nothing / 0 bytes.

I got it from hg since the 2.0 release, not sure how to tell whether
it’s the stable/dev branch.

thanks,


Message: 2
Date: Thu, 19 Sep 2013 23:20:08 -0400
From: Jonathan Dearborn <@Jonathan_Dearborn>
To: SDL Development List
Subject: Re: [SDL] reading files on android
Message-ID:
<CA+DSiHb1DO–AgBzQ1NC1gV27dtKsYy=
1Q13fm-f6Xafyu5sTw at mail.gmail.com>
Content-Type: text/plain; charset=“iso-8859-1”

I’m pretty much doing the same thing. I’m copying an asset from the apk
to
internal storage so that it can be modified by the user (custom game
levels).

The only difference I can see is that my code uses SDL_RWFromFile() for
file_out instead of going through a FILE pointer. What problems are you
experiencing? Does logcat tell you anything interesting? Did you do a
sanity check over your paths and the function results? Are you using the
official SDL 2.0 build or are you using the latest from hg?

Jonny D

On Thu, Sep 19, 2013 at 10:03 PM, Michael Brough wrote:

Hi, I’m struggling to get basic file IO working on android, using
SDL_RWops to decompress files from the .apk. Am I missing something
really obvious?

Here’s the code:
SDL_RWops *file_in = SDL_RWFromFile(filename, “rb”);
sprintf(buf, “%s/%s”, SDL_AndroidGetInternalStoragePath(),filename);
FILE *_file_out = fopen(buf, “wb”);
SDL_RWops *file_out = SDL_RWFromFP(_file_out, SDL_TRUE);
char buffer[CPBUFSIZE];
int size = SDL_RWread(file_in, buffer, 1, CPBUFSIZE);
while (size>0)
{
SDL_RWwrite(file_out, buffer, 1, size);
size = SDL_RWread(file_in, buffer, 1, CPBUFSIZE);
}
SDL_RWclose(file_out);
SDL_RWclose(file_in);

I figured it’d be easier to extract everything to internal storage so
I could leave the rest of the code the same as on other platforms.
But now I’ve tried also just loading them straight in with RWops and
there’s definitely a problem in that part.

Thanks,
M


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Here’s how I ended up doing this for anyone looking.

Code:
SDL_RWops *oldFile = SDL_RWFromFile(oldFileName.c_str(), “rb”);
if(oldFile > 0)
{
SDL_RWops *newFile = SDL_RWFromFile(newFileName.c_str(), “wb”);

	if(!newFile)
		return false;

	char c;
	int size = SDL_RWread(oldFile, &c, 1, 1);

	while(size > 0)
	{
		if(SDL_RWwrite(newFile, &c, 1, size) == 0)
		{
			SDL_RWclose(oldFile);
			SDL_RWclose(newFile);
			return false;
		}

		size = SDL_RWread(oldFile, &c, 1, 1);
	}

	SDL_RWclose(oldFile);
	SDL_RWclose(newFile);
}