title: android ndk, how to read image file to bitmap jobject?
body: <p>I have a Native android project that gets the internal storage directory of the current application and can also manipulate the pixels of a bitmap passed from the java to the c++.</p>
  
  <p>My goal is to be able to read in an image file from that location as a jobject in the C itself, so I can then return to the calling function. The catch is that I wish to avoid using a java call to retrieve the bitmap itself and want it done purely by the Native. (i.e. nothing like env->CallObjectMethod)</p>
  
  <p>The code I have so far gets the location/filename of the desired image file, but I do have another native file which is properly working with/manipulating passed in bitmaps(using bitmap.h), but since it's used afterwards and I'm comfortable with it I haven't included it. Here is the path/filename code:</p>
  
  <pre><code>#include "frameConstruction.h"
  #include &lt;string&gt;
  #include &lt;unistd.h&gt;
  
  using namespace std;
  
  #define  LOG_TAG    "TAG"
  //macros to call the Log functions to allow for debugging. the tag is declared on the line above
  #define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
  #define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
  
  JNIEXPORT jstring JNICALL Java_com_example_ndkbitmapcreation_FrameConstructionLib_constructFrame(
          JNIEnv* env, jobject obj, jobject javaThis, jstring stringSomething) {
  
      jclass cls = env-&gt;GetObjectClass(javaThis);
  
      jmethodID getFilesDir = env-&gt;GetMethodID(cls, "getFilesDir",
              "()Ljava/io/File;");
  
      jobject dirobj = env-&gt;CallObjectMethod(javaThis, getFilesDir);
      jclass dir = env-&gt;GetObjectClass(dirobj);
  
      jmethodID getStoragePath = env-&gt;GetMethodID(dir, "getAbsolutePath",
              "()Ljava/lang/String;");
      jstring path = (jstring) env-&gt;CallObjectMethod(dirobj, getStoragePath);
      const char *pathstr = env-&gt;GetStringUTFChars(path, 0);
      std::string fullPath = std::string(pathstr);
      chdir(pathstr);
  
      env-&gt;ReleaseStringUTFChars(path, pathstr);
  
      const char *filenamestr = env-&gt;GetStringUTFChars(stringSomething, 0);
      //need code here to call the File.separator method from java,
      //if someone knows how to do this that'd be great, currently
      //this just grabs the first char from the path which is (hopefully)
      //the proper file separator
      fullPath += fullPath[0];
      fullPath += filenamestr;
  
      env-&gt;ReleaseStringUTFChars(stringSomething, filenamestr);
      //pixel manip will go here
  
      return env-&gt;NewStringUTF(fullPath.c_str());
  }
  </code></pre>
  
  <p>so how would I go about reading the data from that file into a jobject returnable as a bitmap (i.e.: return varName;) without calling a java method? Alternatively I could read it into a bytearray like from: <a href="https://stackoverflow.com/questions/12854333/jni-in-c-to-read-file-to-jbytearray">JNI in C++ to read file to jbyteArray</a> then the question becomes how to change a jbytearray into a jobject usable with  again without calls to the Java?</p>
  
  <p>I'm hoping to avoid making a bitmap in java then pass it into c++, but my code requires a lot of bitmap loading/manipulating and is causing OOM errors in the java, so I'm trying an approach in Native to try my best to avoid it. It's been a many month long project and this is hopefully one of the final hurdles, if anyone out there can help I'd sure appreciate it ^_^.</p>