# Reading files on android

**URL:** <https://discourse.libsdl.org/t/reading-files-on-android/18437>\
**Category:** SDL Development\
**Created:** [September 2, 2011, 5:51am UTC](https://discourse.libsdl.org/t/reading-files-on-android/18437 "2011-09-02T05:51:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kazzmir](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/kazzmir/32/2392_2.png) [@kazzmir](https://discourse.libsdl.org/u/kazzmir)\
**Post date:** [September 2, 2011, 5:51am UTC](https://discourse.libsdl.org/t/reading-files-on-android/18437/1 "2011-09-02T05:51:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kazzmir](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/kazzmir/32/2392_2.png) [@kazzmir](https://discourse.libsdl.org/u/kazzmir)\
**Post date:** [September 2, 2011, 5:57pm UTC](https://discourse.libsdl.org/t/reading-files-on-android/18437/2 "2011-09-02T17:57:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_Brough](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/michael_brough/32/2433_2.png) [@Michael\_Brough](https://discourse.libsdl.org/u/Michael_Brough)\
**Post date:** [September 20, 2013, 2:03am UTC](https://discourse.libsdl.org/t/reading-files-on-android/18437/3 "2013-09-20T02:03:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JonnyD](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/jonnyd/32/1604_2.png) [@JonnyD](https://discourse.libsdl.org/u/JonnyD)\
**Post date:** [September 20, 2013, 3:20am UTC](https://discourse.libsdl.org/t/reading-files-on-android/18437/4 "2013-09-20T03:20:08Z")

</div>

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)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![Michael\_Brough](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/michael_brough/32/2433_2.png) [@Michael\_Brough](https://discourse.libsdl.org/u/Michael_Brough)\
**Post date:** [September 20, 2013, 5:35pm UTC](https://discourse.libsdl.org/t/reading-files-on-android/18437/5 "2013-09-20T17:35:39Z")

</div>

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](http://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)  
> > [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![JonnyD](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/jonnyd/32/1604_2.png) [@JonnyD](https://discourse.libsdl.org/u/JonnyD)\
**Post date:** [September 22, 2013, 3:28am UTC](https://discourse.libsdl.org/t/reading-files-on-android/18437/6 "2013-09-22T03:28:17Z")

</div>

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](http://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)  
> > > [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![ronkrepps](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/r/85f322/32.png) [@ronkrepps](https://discourse.libsdl.org/u/ronkrepps)\
**Post date:** [January 19, 2014, 2:29pm UTC](https://discourse.libsdl.org/t/reading-files-on-android/18437/7 "2014-01-19T14:29:50Z")

</div>

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);
}
```
