Android fopen usage

  • 2198
  • 0
  • 2013-04-18

摘要:Android fopen usage

reference http://skwtchen.blogspot.tw/2011/01/ndkassets.html

there are 2 ways to use fopen 

1st

將asset或res中檔案的附檔名存成.mp3或.png之類,來避免打包成APK時檔案被壓縮。取得file descriptor, offset, 及length並透過JNI傳遞到C code。從Java File Descriptor object中取得OS file descriptor,並使用fdopen及fseek取得所要的檔案。

the files u want to use fopen u have to ensure the files's extension must be .mp3 or .png or it will be compressed by zip

then fetch file descriptor in jave code

 

2nd

首先在java code中要先取得APK檔的所在路徑,並將之透過JNI傳給C code,接下來就可以透過libzip所提供的function,像是zip_open, zip_fopen, zip_fread等來讀取所需要的檔案啦!! 下面的範例是把APK檔中所有檔案print出來。

get apk file full path by jin ,and use libzup to get ur things

=============================================================

if u are using nativeActive u could do as below

        c++ call java by jni

                  it will be like this

 

                   jclass thisClass = mApp->appThreadEnv->GetObjectClass(mApp->appThreadThis);
                   jmethodID showToastAlert = mApp->appThreadEnv->GetMethodID(thisClass, "showToastAlert", "(Ljava/lang/String;)V");
                  mApp->appThreadEnv->CallVoidMethod(mApp->appThreadThis, showToastAlert, jniText);

 

        jni call c++

                   it will be like this

 

 
         public class NativeSubclass extends NativeActivity
         {
              public void showToastAlert(String text)
              {
                   final String finalText = text;
                   runOnUiThread(new Runnable() {
                   public void run()
                  {
                       Toast.makeText(getApplicationContext(), finalText, Toast.LENGTH_SHORT).show();
                  }
               }
          )

 

           below is how to get apkpath

ApplicationInfo appInfo = null;
PackageManager packMgmr = this.getPackageManager();
try {
    appInfo = packMgmr.getApplicationInfo("com.skwt.gl2jni", 0);
} catch (NameNotFoundException e) {
    e.printStackTrace();
    throw new RuntimeException("Unable to locate assets, aborting...");
}
String apkFilePath = appInfo.sourceDir;
GL2JNILib.initAPK(apkFilePath);
 

 

 

or jusr simplify to do this in ur cpp

 

http://stackoverflow.com/questions/7701801/obtaining-the-name-of-an-android-apk-using-c-and-the-nativeactivity-class

 

 

jclass clazz = e_pThreadEnv->GetObjectClass(e_pActivity->clazz);
jmethodID methodID = e_pThreadEnv->GetMethodID(clazz, "getPackageCodePath", "()Ljava/lang/String;");
jobject result = e_pThreadEnv->CallObjectMethod( e_pActivity->clazz, methodID);
const char* str;
jboolean isCopy;
str = e_pThreadEnv->GetStringUTFChars((jstring)result, &isCopy);
 
 
while u got apk path,use libz to use fopen
reference
http://blog.sephiroth.it/2010/10/24/reading-resource-files-from-native-code/