2

In my Android project, I want to access a drawable file using c/c++ from the android-ndk, like R.drawable.some_drawable.xml.

How can I achieve such functionality ?

Farhad
  • 12,178
  • 5
  • 32
  • 60
  • you cannot, what would you like to do that for? – pskink Jul 31 '17 at 08:37
  • I have a file which I don't like putting in assets or raw folder. Any suggestions ? – Farhad Jul 31 '17 at 08:46
  • so read it from c/c++ using the full path, but you will have access to raw data, not to any `Drawable` object – pskink Jul 31 '17 at 08:47
  • just wanted to make sure if any api is available already. The raw data is just fine. – Farhad Jul 31 '17 at 08:49
  • here you have all the apis: https://developer.android.com/ndk/guides/stable_apis.html – pskink Jul 31 '17 at 08:50
  • Easiest way would be to pass it to JNI as a bitmap. See https://stackoverflow.com/questions/32603218/the-fastest-way-to-pass-java-bitmap-pixels-to-jni/32619036#32619036 – WLGfx Jul 31 '17 at 09:09
  • @WLGfx `R.drawable.some_drawable.xml` as a `Bitmap`? – pskink Jul 31 '17 at 09:13
  • Yes: https://stackoverflow.com/questions/3035692/how-to-convert-a-drawable-to-a-bitmap – WLGfx Jul 31 '17 at 09:16
  • @WLGfx he wants to access xml file (or `Drawable` object), not a `Bitmap` – pskink Jul 31 '17 at 09:22
  • Ah yes, i went to quickly on that. An xml drawable can be converted to a bitmap. Hopefully this helps: https://stackoverflow.com/a/35574775/2979092 – WLGfx Jul 31 '17 at 09:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150601/discussion-between-wlgfx-and-pskink). – WLGfx Jul 31 '17 at 11:37

1 Answers1

1

AFAIK, using JNI you are limited to some primitive data types:

The mapping between Java data types and data types used in native code is pretty straightforward. The naming scheme remains the same: the native data type name is preceded by the character 'j', followed by all lowercase data type name equivalent to Java. JNI also includes another data type named jsize, which stores the length or an array or string.

void -> void : None
byte -> jbyte : 8-bit signed. Range is -27 to 27 - 1
int -> jint : 32-bit signed. Range is -231 to 231 - 1
float -> jfloat : 32 bits. Represent a real number as small as 1.4 x 10-45 and as big as 3.4 x 1038 (approx.), positive or negative
double -> jdouble : 64 bits. Represent a real number as small as 4.9 x 10-324 and as big as 1.7 x 10308 (approx.), positive or negative
char -> jchar : 16-bit unsigned. Range is 0 to 65535
long -> jlong : 64-bit signed. Range -263 to 263 - 1
short -> jshort : 16-bit signed. Range is -215 to 215 - 1
boolean -> jboolean : Unsigned 8 bits. true and false

Your solution would be saving your Drawable in cache folder and pass file address to your NDK code and read it in C/C++.

How can I write a Drawable resource to a File?

Mohsen Mirhoseini
  • 8,454
  • 5
  • 34
  • 59