Question

Java code:

    package local.ttt;

    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Typeface;

    public class Text2Bitmap {
        static Bitmap getBitmap(String text,int fontsize) {
            Paint paint=new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.WHITE);
            paint.setTextSize(fontsize);
            paint.setAntiAlias(true);
            paint.setTypeface(Typeface.DEFAULT);

            int outwidth=(int)Math.round(paint.measureText(text));
            Bitmap bitmap=Bitmap.createBitmap(outwidth,fontsize,Bitmap.Config.ALPHA_8);

            Canvas canvas=new Canvas(bitmap);
            canvas.drawText(text,0,fontsize-1,paint);

            return bitmap;
        }
    }

C source:


            ...
            MY_ASSERT(vm_cached!=NULL);
            JNIEnv* env;
            jint res=vm_cached->AttachCurrentThread(&env,NULL);
            MY_ASSERT(res==0);
            jclass activityClass=env->FindClass("android/app/NativeActivity");
            MY_ASSERT(activityClass!=NULL);
            jmethodID getClassLoader=env->GetMethodID(activityClass,"getClassLoader","()Ljava/lang/ClassLoader;");
            MY_ASSERT(getClassLoader!=NULL);
            MY_ASSERT(nativeActivityObjHnd!=NULL);
            jobject cls=env->CallObjectMethod(nativeActivityObjHnd,getClassLoader);
            jclass classLoader=env->FindClass("java/lang/ClassLoader");
            MY_ASSERT(classLoader!=NULL);
            jmethodID findClass=env->GetMethodID(classLoader,"loadClass","(Ljava/lang/String;)Ljava/lang/Class;");
            MY_ASSERT(findClass!=NULL);
            jstring strClassName=env->NewStringUTF("Llocal/ttt/Text2Bitmap;");
            jclass text2bitmapClass=(jclass)env->CallObjectMethod(cls,findClass,strClassName); //fails here
            MY_ASSERT(text2bitmapClass!=NULL);
            res=vm_cached->DetachCurrentThread();
            MY_ASSERT(res==0);
            ...

it fails with:

W/dalvikvm(5614): dvmFindClassByName rejecting 'Llocal/ttt/Text2Bitmap;'
W/dalvikvm(5614): threadid=9: thread exiting with uncaught exception (group=0x4002d560)
E/AndroidRuntime(5614): FATAL EXCEPTION: Thread-10
E/AndroidRuntime(5614): java.lang.ClassNotFoundException: Llocal/ttt/Text2Bitmap; in loader dalvik.system.PathClassLoader[.]
E/AndroidRuntime(5614):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
E/AndroidRuntime(5614):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
E/AndroidRuntime(5614):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
E/AndroidRuntime(5614):     at dalvik.system.NativeStart.run(Native Method)

Any hint what I'm doing wrong, please?

Was it helpful?

Solution

Don't mess with java class loader. Use this code:

MY_ASSERT(vm_cached!=NULL);

JNIEnv* env;
jint res = vm_cached->AttachCurrentThread(&env,NULL);
MY_ASSERT(res==0);

jclass t2bClass = env->FindClass("local/ttt/Text2Bitmap");
MY_ASSERT(t2bClass!=NULL);

jmethodID getBitmap = env->GetStaticMethodID(t2bClass, "getBitmap","(Ljava/lang/String;I)Landroid/graphics/Bitmap;");
MY_ASSERT(getBitmap!=NULL);

jstring text = ...;
int fontsize = ...;
jobject bitmap = env->CallStaticObjectMethod(t2bClass, getBitmap, text, fontsize);
MY_ASSERT(bitmap!=NULL);

// process bitmap here
// ...

res=vm_cached->DetachCurrentThread();
MY_ASSERT(res==0);

OTHER TIPS

Not sure what is causing your error (you do not indicate where the lines are, or the particular line where you are having difficulty debugging), but I will go ahead and point out a few quick mistakes I see.

One first mistake is you need to use CallStaticObjectMethod since you are calling a static method. Your jmethodid findClass has a methodID, not a class, which is misleading and wrong. You also have two parameters, but you seem to be only passing one parameter.

Basically, you need:

(env)->GetStaticMethodID(jclass,"method name", "Parameter list")

--jclass is the class reference --"method name" is the name of the method IN quotes --"Parameter list" is the list of parameters, which you need to look up syntax.

Skipping a few things...To call your stuff you need:

(env)->CallStaticObjectMethod(jclass,jmid,parameter1,parameter2)

--jclass is once again the class that has the static method. --jmid is the java method id which you get from the function above. --parameter1 and parameter2 are the parameters required for the java method.

Lastly, I think this may just be preference, but I would not attach the thread to the JVM until you have all the necessary information to make the jump into the JVM.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top