<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>resource &#8211; Alessandro Crugnola &#8211; sephiroth.it</title>
	<atom:link href="https://blog.sephiroth.it/tag/resource/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.sephiroth.it</link>
	<description>programmer by pure chance</description>
	<lastBuildDate>Tue, 26 Oct 2010 11:08:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://blog.sephiroth.it/wp-content/uploads/2019/03/cropped-sepy-big-32x32.png</url>
	<title>resource &#8211; Alessandro Crugnola &#8211; sephiroth.it</title>
	<link>https://blog.sephiroth.it</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Reading resource files from native code</title>
		<link>https://blog.sephiroth.it/2010/10/24/reading-resource-files-from-native-code/</link>
					<comments>https://blog.sephiroth.it/2010/10/24/reading-resource-files-from-native-code/#comments</comments>
		
		<dc:creator><![CDATA[Alessandro Crugnola]]></dc:creator>
		<pubDate>Sun, 24 Oct 2010 10:50:19 +0000</pubDate>
				<category><![CDATA[android]]></category>
		<category><![CDATA[assets]]></category>
		<category><![CDATA[native]]></category>
		<category><![CDATA[ndk]]></category>
		<category><![CDATA[resource]]></category>
		<category><![CDATA[zip]]></category>
		<guid isPermaLink="false">http://blog.sephiroth.it/?p=1401</guid>

					<description><![CDATA[If you&#8217;re working with android-ndk and you want to open an asset included in your apk package, you should make a request via jni to the Resource manager in your java code. Something like: [cc] public int[] openAsset( String path ) { AssetFileDescriptor ad = null; try { ad = getResources().getAssets().openFd( path ); Integer off &#8230; <a href="https://blog.sephiroth.it/2010/10/24/reading-resource-files-from-native-code/" class="more-link">Continue reading <span class="screen-reader-text">Reading resource files from native code</span></a>]]></description>
										<content:encoded><![CDATA[<p>If you&#8217;re working with android-ndk and you want to open an asset included in your apk package, you should make a request via jni to the Resource manager in your java code.<br />
Something like:</p>
<p>[cc]<br />
public int[] openAsset( String path )<br />
{<br />
	AssetFileDescriptor ad = null;<br />
	try<br />
	{<br />
		ad = getResources().getAssets().openFd( path );<br />
		Integer off = (int) ad.getStartOffset();<br />
		Integer len = (int) ad.getLength();<br />
		int res[] = { off, len };<br />
		ad.close();<br />
		return res;<br />
	} catch( IOException e ) {<br />
		Log.e( TAG, e.toString() );<br />
	}<br />
	return null;<br />
}<br />
[/cc]
</p>
<p>However this is not a optimal solution expecially when you have tons of file io operations, also because I prefer to limit the amount of calls between java and C++.<br />
A better solution I&#8217;m using is to open the current apk application using the <a href="http://www.nih.at/libzip/" target="_blank">libzip</a> library. Just download the library and compile as static library, then include it in your Android.mk make file under the <strong>LOCAL_STATIC_LIBRARIES</strong> section.<br />
What you have to do first is to send the current application filename from java, once at startup, in this way:</p>
<p>[cc]<br />
PackageInfo info = null;<br />
try {<br />
	info = getContext().getPackageManager().getPackageInfo(&#8220;com.example.text&#8221;, 0);<br />
} catch( NameNotFoundException e ) {<br />
	Log.e( TAG, e.toString() );<br />
	return;<br />
}<br />
setAppName( info.applicationInfo.sourceDir );<br />
[/cc]</p>
<p>and in the C++ code you will have the corresponding <strong>setAppName</strong> method:</p>
<p>[cc]<br />
#include <zip .h><br />
#include <zipint .h><br />
#include <jni .h></p>
<p>char packageName[1024] = {0};<br />
zip	*pkg_zip;</p>
<p>JNIEXPORT void JNICALL Java_com_example_test_MySurfaceView_setAppName( JNIEnv * env, jobject obj, jstring pkgname )<br />
{<br />
	const char *buffer = env->GetStringUTFChars( pkgname, false );</p>
<p>	int error;<br />
	pkg_zip = zip_open( buffer, 0, &#038;error );<br />
	strcpy( packageName, buffer );<br />
	if( pkg_zip == NULL ){<br />
		LOGE(&#8220;Failed to open apk: %i&#8221;, error );<br />
	}<br />
	env->ReleaseStringUTFChars( pkgname, buffer );<br />
}<br />
[/cc]</jni></zipint></zip></p>
<p>Now you have a reference to the zip package. In this way now you can query the zip package asking if a particular file exists:</p>
<p>[cc]<br />
bool FileExists(const char* fname)<br />
{<br />
	if( pkg_zip != NULL )<br />
	{<br />
		int result = zip_name_locate( pkg_zip, fname, 0);<br />
		return result != -1;<br />
	}<br />
	return false;<br />
}<br />
[/cc]</p>
<p>And open any of the files included in your apk package:</p>
<p>[cc]<br />
FILE *FileOpen( const char* fname )<br />
{<br />
	zip_file *zfile = zip_fopen( pkg_zip, fname, 0 );<br />
	uint32_t offset = 0;<br />
	uint32_t length = 0;</p>
<p>	if( zfile != NULL )<br />
	{<br />
		offset = zfile->fpos;<br />
		length = zfile->bytes_left;<br />
		zip_fclose( zfile );<br />
		zfile = NULL;<br />
	} else<br />
	{<br />
		return NULL;<br />
	}</p>
<p>	FILE *fp = NULL;<br />
	fp = fopen( packageName, &#8220;rb&#8221; );<br />
	fseek( fp, offset, SEEK_SET );<br />
	return fp;<br />
}<br />
[/cc]</p>
<p>Using this method you will have a FILE pointer to the whole apk file so you have to take care of the <strong>offset</strong> and <strong>length</strong> field when reading and seeking the file itself.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.sephiroth.it/2010/10/24/reading-resource-files-from-native-code/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
	</channel>
</rss>
