Re: How to pass resource identifiers from native code

221 views
Skip to first unread message

Yamusani Vinay

unread,
May 29, 2013, 9:37:21 AM5/29/13
to andro...@googlegroups.com
In java Code:
                    AssetFileDescriptor  afd = getResources().openRawResourceFd(R.raw.low);
  FileDescriptor fd =afd.getFileDescriptor();
   int offset = (int)afd.getStartOffset();
int length = (int)afd.getLength();
methodName(fd,offset,length);--->calling native method.

In C code:


jclass fdClass = env->FindClass("java/io/FileDescriptor");

if (fdClass != NULL) {

jfieldID fdClassDescriptorFieldID = env->GetFieldID(fdClass,
"descriptor", "I");
if (fdClassDescriptorFieldID != NULL && fd_sys != NULL) {
jint fd = env->GetIntField(fd_sys, fdClassDescriptorFieldID);
myfd = dup(fd);

}
        else {
__android_log_print(ANDROID_LOG_INFO, "err", "fd sys null");
}
} else {
__android_log_print(ANDROID_LOG_INFO, "err", "fd class null");
}


On Wed, May 29, 2013 at 4:25 PM, Steven Venter <sven...@gmail.com> wrote:
Hi all

I'm trying open a file in res/raw from native code - NativeActivity.

Here is a section of code:

jmethodID getResourcesId = env->GetMethodID(contextClass, "getResources",
"()Landroid/content/res/Resources;");
jobject resourcesObj = env->CallObjectMethod(app->activity->clazz,
getResourcesId);
jclass resourcesCls = env->FindClass("android/content/res/Resources");
if (!resourcesCls) {
__android_log_print(ANDROID_LOG_ERROR, "AccessingApkFiles",
"Can't find android/content/res/Resources");
} else {
jmethodID getOpenRawResId = env->GetMethodID(resourcesCls,
"openRawResourceFd", "(I)Landroid/content/res/AssetFileDescriptor;");
if (getOpenRawResId) {
__android_log_print(ANDROID_LOG_WARN, "AccessingApkFiles",
"-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-");
jobject assetFileDescriptorObj = env->CallObjectMethod(resourcesCls,
getOpenRawResId, "R/res/raw");
} else
__android_log_print(ANDROID_LOG_ERROR, "AccessingApkFiles",
"Can't get method ID for openRawResourceFd");
}

When the bold section of code is executed I get the following error:

W/dalvikvm(6979): JNI WARNING: can't call Landroid/content/res/Resources;.openRawResourceFd on instance of Ljava/lang/Class;

I think the problem is with the resource identifier that needs to be passed the Resource object.

How do you pass the resource identifier to the method. Java is R.res.raw but for native code?

Thanks

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-ndk?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Steven Venter

unread,
May 29, 2013, 11:28:45 AM5/29/13
to andro...@googlegroups.com
Thanks Yamusani

But I'm using  NativeActivity.

I'm trying the use the getIdentifier call like this:

jmethodID getIdentifierID = env->GetMethodID(resourcesCls, "getIdentifier",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I");
if (getIdentifierID) {
jstring name = env->NewStringUTF("mypng.png");
jstring defType = env->NewStringUTF("drawable");
jstring defPackage = env->NewStringUTF("com.bla.accessingapkfiles");

jint resRawId = env->CallIntMethod(resourcesObj, getIdentifierID,
name, defType, defPackage);

if (resRawId == 0) {
__android_log_print(ANDROID_LOG_WARN, "AccessingApkFiles",
"No dude this does not work: [%d]", resRawId);
} else {
jmethodID getOpenRawResId = env->GetMethodID(cls,
"openRawResourceFd", "(I)Landroid/content/res/AssetFileDescriptor;");
if (getOpenRawResId) {

but I can't get it to work. The resRawId is always 0.

Has someone tried this before?
Reply all
Reply to author
Forward
0 new messages