简体   繁体   English

来自JNI中资源Uri的文件

[英]File from resource Uri in JNI

Hi I'm trying to get a File object from the Uri of a drawable resource. 嗨,我正在尝试从可绘制资源的Uri获取File对象。 I'm using this code: 我正在使用此代码:

jclass clazz;
jmethodID methodID;
jstring string1, string2, string3;
jobject object1, object2;

clazz = env->FindClass("android/net/Uri");
methodID = env->GetStaticMethodID(clazz, "parse", "(Ljava/lang/String;)Landroid/net/Uri;");
string1 = env->NewStringUTF("android.resource://com.appsite.android/drawable/grass");
object1 = env->CallStaticObjectMethod(clazz, methodID, string1);
methodID = env->GetMethodID(clazz, "toString", "()Ljava/lang/String;");
string1 = (jstring)env->CallObjectMethod(object1, methodID);

clazz = env->FindClass("java/net/URLEncoder");
methodID = env->GetStaticMethodID(clazz, "encode", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
string2 = (jstring)env->NewStringUTF("UTF-8");
string3 = (jstring)env->CallStaticObjectMethod(clazz, methodID, string1, string2);


clazz = env->FindClass("java/net/URI");
methodID = env->GetMethodID(clazz, "<init>", "(Ljava/lang/String;)V");
object1 = env->NewObject(clazz, methodID, string3);

clazz = env->FindClass("java/io/File");
methodID = env->GetMethodID(clazz, "<init>", "(Ljava/net/URI;)V");
object2 = env->NewObject(clazz, methodID, object1);

The application crashes at the last call to NewObject . 该应用程序在最后一次调用NewObject崩溃。 What are the possible reasons for that? 可能的原因是什么?

EDIT : 编辑

It seems like a problem with passing a jobject as parameter to NewObject like that. 像这样将jobject作为参数传递给NewObject似乎是一个问题。 What is the proper way to pass a jobject to NewObject ? jobject传递给NewObject的正确方法是什么?

This is not related to JNI. 这与JNI不相关。

The problem is that there no java.io.File for the drawable resource. 问题在于可绘制资源没有java.io.File The grass drawable is embedded in the APK in PNG or other image format, and the resource manager extracts the bitmap for Resources.getDrawable(int) by looking into the APK directly. 草地可绘制对象以PNG或其他图像格式嵌入到APK中,资源管理器通过直接查看APK来提取Resources.getDrawable(int)的位图。

If you need to access your bitmap with file i/o methods, the closest you can get is thorough Resources.openRawResourceFd() method (limited to uncompressed resources). 如果您需要使用文件I / O方法访问位图,则最接近的方法是使用Resources.openRawResourceFd()方法(仅限于未压缩的资源)。 You can also put the bitmap in assets instead of res . 您还可以将位图放置在资产中,而不是res中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM