/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_fileText_fileRead */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>#ifndef _Included_com_fileText_fileRead
#define _Included_com_fileText_fileRead
#ifdef __cplusplus#include "fileReader.h"fileReader m_reader;jstring chartojstring( JNIEnv* env, char* pat )
{
   //__android_log_print(ANDROID_LOG_WARN, "wuzhenhua", "change1");
    jclass strClass = env->FindClass("java/lang/String");
    jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
    jbyteArray bytes = env->NewByteArray(strlen(pat));
    env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
    jstring encoding = env->NewStringUTF("UTF-8");
    return (jstring)env->NewObject(strClass, ctorID, bytes, encoding);
}extern "C" {
#endif/*
 * Class:     com_fileText_fileRead
 * Method:    loadFile
 * Signature: (Ljava/lang/String;)Z
 */
JNIEXPORT jboolean JNICALL Java_com_fileText_fileRead_loadFile
  (JNIEnv *env, jobject, jstring filePath)
  {
     char *file =(char*)env->GetStringUTFChars(filePath,0);
 return  m_reader.loadFile(file);
  }/*
 * Class:     com_fileText_fileRead
 * Method:    GetBuf
 * Signature: (Ljava/lang/String;)V
 */
JNIEXPORT jstring JNICALL Java_com_fileText_fileRead_GetBuf
  (JNIEnv *env, jobject, jint size)
  {
     char *buffer = new char[size];
 memset(buffer,0,size);
     m_reader.GetfileBuf(buffer);
 
 jstring buf = chartojstring(env,buffer);
 
 char *file =(char*)env->GetStringUTFChars(buf,0);
 FILE *fp =NULL;
 fp = fopen("/sdcard/110.txt","wr");
 fwrite(file,sizeof(char),strlen(file),fp);
 fclose(fp);
 return buf;
  }#ifdef __cplusplus
}
#endif
#endif目的是在Eclipse 端传文件路径,C++端读数据再返回回去;
fileReader 是C++的一个类,没问题。
通过测试当读txt文档时,可以通过测试,但是当我读png图片时,返回的是前几个字节,不完整,
我怀疑是chartojstring()这个函数出问题了,懂得人帮看看。

解决方案 »

  1.   

    1,png文件数据城肯定有字节是0x0的,你用字符串来存是会断掉的,要用byte[]
    2,注意编码问题
      

  2.   

    楼上说的对 PNG编解码应该是byte的 
      

  3.   

    顶三楼的,图像送数据是不能用string存放的,string 还会涉及编码方式的问题。
      

  4.   

    现在我用Byte[]都接收到了,又遇到编码问题,在Android 端怎么把byte[]里的数据解析?
      

  5.   

    如果读的是png二进制文件,那就不要用字符串了,字符串的定义是以0结尾的,当遇到0的时候就结束了。
    应该用byte 对应你的数据,而不是用string。
      

  6.   


    png文件你还要怎么解析?可以直接转成对应的bitmap对象
      

  7.   

    修改那个函数,不使用 strlen,直接把长度作为参数传递过去就没事了
      

  8.   

    使用参数吧长度传递过去,JNI有自己的类型