Android+JNI调用–文件操作(附源码)(3)

3.       本地接口实现:
根据对应的.h文件实现其接口。
JNI_FileSys.c代码:
#include"JNI_FileSys.h"
#define LOG_TAG"JNI_FileSys"
#define  LOGI(...) __Android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define  LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#include<android/log.h>
#include"FileSys.h"

/*
* Class:    com_luoxudong_jni_reader_FileSys
* Method:   FileOpen
* Signature: ([BI)I
*/
JNIEXPORT jintJNICALL Java_com_luoxudong_jni_reader_FileSys_FileOpen
  (JNIEnv *env, jclass jobj, jstring pFileName,jint openMode)
{
              UINT8 *pbyFileName = (UINT8*)(*env)->GetStringUTFChars(env, pFileName, 0);
              LOGI("file name:%s---opentype:%d", pbyFileName, openMode);
              return FileOpen(pbyFileName,openMode);

}

/*
* Class:    com_luoxudong_jni_reader_FileSys
* Method:   FileLength
* Signature: (I)I
*/
JNIEXPORT jintJNICALL Java_com_luoxudong_jni_reader_FileSys_FileLength
  (JNIEnv *env, jclass jobj, jint fd)
{
              return FileLength(fd);
}

/*
* Class:    com_luoxudong_jni_reader_FileSys
* Method:   FileSeek
* Signature: (III)I
*/
JNIEXPORT jintJNICALL Java_com_luoxudong_jni_reader_FileSys_FileSeek
  (JNIEnv *env, jclass jobj, jint fd, jintoffset, jint origin)
{

              return FileSeek(fd, offset,origin);
}

/*
* Class:    com_luoxudong_jni_reader_FileSys
* Method:   FileRead
* Signature:(II)Lcom/luoxudong/jni/bean/CusBuffer;
*/
JNIEXPORT jobjectJNICALL Java_com_luoxudong_jni_reader_FileSys_FileRead
  (JNIEnv *env, jclass jobj, jint fd, jintcount)
{
              int nReadLen = 0;
              UINT8 *pBuf = (UINT8*)malloc(count);
              memset(pBuf, 0, count);

              nReadLen = FileRead(fd, pBuf,count);

              jbyte *pBy = (jbyte *)pBuf;
              jbyteArray jarray =(*env)->NewByteArray(env, nReadLen);
              (*env)->SetByteArrayRegion(env,jarray, 0, nReadLen, pBy);             
              jclass    m_cls = (*env)->FindClass(env,"com/luoxudong/jni/bean/CusBuffer");
              jmethodID m_mid =(*env)->GetMethodID(env, m_cls, "<init>", "()V");
              jfieldID  m_fid1 = (*env)->GetFieldID(env, m_cls,"buffer", "[B");
              jfieldID  m_fid2 = (*env)->GetFieldID(env, m_cls,"nBufferLen", "I");

              jobject   m_obj = (*env)->NewObject(env, m_cls,m_mid);
              (*env)->SetObjectField(env,m_obj, m_fid1, jarray);
              (*env)->SetIntField(env, m_obj,m_fid2, nReadLen);
              return m_obj;
}

/*
* Class:    com_luoxudong_jni_reader_FileSys
* Method:   FileWrite
* Signature: (I[BI)I
*/
JNIEXPORT jintJNICALL Java_com_luoxudong_jni_reader_FileSys_FileWrite
  (JNIEnv *env, jclass jobj, jint fd,jbyteArray buf, jint count)
{
              jbyte *pjb = (jbyte*)(*env)->GetByteArrayElements(env, buf, 0);
              jsize len =(*env)->GetArrayLength(env, buf);
              UINT8 *byBuf = (UINT8 *)pjb;
              pjb[len] = '\0';
              return FileWrite(fd, byBuf,count);
}

/*
* Class:    com_luoxudong_jni_reader_FileSys
* Method:   FileClose
* Signature: (I)I
*/
JNIEXPORT jintJNICALL Java_com_luoxudong_jni_reader_FileSys_FileClose
  (JNIEnv *env, jclass jobj, jint fd)
{
              return FileClose(fd);
}
其中头部定义了些andriod中日志输出所需要的宏,以及其他关联的.h文件,在一些地方C跟C++的使用语法不太一样,如在C中调用UINT8 *pbyFileName = (UINT8*)(*env)->GetStringUTFChars(env, pFileName, 0),
而C++中的语法为env-> (UINT8 *)GetStringUTFChars(pFileName,0)。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwywgz.html