Member-only story

How To Read an Image File in C++ in Android With NDK

Different approaches to reading any file directly in the native layer in Android

Minhaz
Better Programming
8 min readNov 12, 2022

--

Photo by Olaf Val on Unsplash.

Software development in Android can be done using the Java SDK or Native Development Kit, aka NDK, made available by Android Open Source Project (AOSP). NDK is often used for writing high-performance code like image processing algorithms.

Many apps have requirements to read files from disk. For reading image files, the usual approach is to read files using Java APIs that are available in Android SDK or use higher-level abstractions like MediaStore APIs. I won’t cover information on reading different file formats in the Java layer in this article.

Sometimes, processing the image files in the native layer (C++) may be needed. In such cases, the usual approach is to do the following:

  • Load the image as a Bitmap.
  • Marshall it to the native layer with JNI.
  • Do read/write operations in the native layer.

However, under certain circumstances, you may want to read the image directly in the native layer. If you have such circumstances — this article is for you!

FYI: When I say “native layer” or “native code,” it means in C++ code. I may use these terms

--

--

Minhaz
Minhaz

Written by Minhaz

Staff Software Engineer @Google. Leading teams democratising On device AI for for masses. Writes about programming generics & specifics.

No responses yet