Write native methods, such as:
package com.example.renzhenming.appmarket.ui.selectimage; import android.graphics.Bitmap; public class ImageUtil { static { System.loadLibrary("jpeg"); System.loadLibrary("compressimg"); } /** * Picture compression * @param quality Quality of Compression * @param fileName Compressed path */ public static void compressImage(Bitmap bitmap,int quality, String fileName){ compressBitmap(bitmap,quality,fileName); } /** * NDK Method to load pictures * @param quality Quality of Compression * @param fileName Compressed path * @return */ public native static int compressBitmap(Bitmap bitmap,int quality, String fileName); /*public static Bitmap decodeFile(String path) { int finalWidth = 800; // Get the width first BitmapFactory.Options options = new BitmapFactory.Options(); // Do not load pictures into memory, only take width and height options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path,options); int bitmapWidth = options.outWidth; int inSampleSize = 1; if(bitmapWidth>finalWidth){ inSampleSize = bitmapWidth/finalWidth; } options.inSampleSize = inSampleSize; options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(path,options); }*/ }
cmd goes into the build/intermediates/classes/debug file directory and enters the following commands
C:\Users\renzhenming\Desktop\appdemo\AppMarketDemo\app\build\intermediates\classes\debug>javah -classpath D:\application\java\sdk\platforms\android-26\android.jar;. -jni com.example.renzhenming.appmarket.ui.selectimage.ImageUtil
The - classpath function is to introduce classes in Android libraries, because it's obvious that the bitmap class in Android is used in our example above, and if we don't add - classpath, we will report errors.
Error: Class android.graphics.Bitmap could not be found. (No need to add if not used)
Note that the semicolons and points behind must be added, otherwise they cannot be found.

After writing, make project and generate. h files in folders (without make, maybe)

In this way, we can get the header file and program in c language.
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_example_renzhenming_appmarket_ui_selectimage_ImageUtil */ #ifndef _Included_com_example_renzhenming_appmarket_ui_selectimage_ImageUtil #define _Included_com_example_renzhenming_appmarket_ui_selectimage_ImageUtil #ifdef __cplusplus extern "C" { #endif /* * Class: com_example_renzhenming_appmarket_ui_selectimage_ImageUtil * Method: compressBitmap * Signature: (Landroid/graphics/Bitmap;ILjava/lang/String;)I */ JNIEXPORT jint JNICALL Java_com_example_renzhenming_appmarket_ui_selectimage_ImageUtil_compressBitmap (JNIEnv *, jclass, jobject, jint, jstring); #ifdef __cplusplus } #endif #endif