如何将文件路径从.java类文件传递到本机jni文件
I want to pass a file path from Java class to native file using jni. In the native implementation file I want to check if file exists or not.
我想使用jni将文件路径从Java类传递到本机文件。在本机实现文件中,我想检查文件是否存在。
This is my java file:
这是我的java文件:
public class FileNative extends Activity {
String fpath = "storage/emulated/0/Download/view.pdf";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str1 =null;
Log.d("Click","The button has been clicked to open the file");
str1 = ndkopenfile(fpath);
}
}
public native String ndkopenfile(String fpath);
static {
System.loadLibrary("file-jni");
}
}
This is my C++ file:
这是我的C ++文件:
JNIEXPORT jstring JNICALL Java_com_example_filewithnative_FileNative_ndkopenfile(JNIEnv *env, jobject obj , jstring path)
{
const char* jnamestr = env->GetStringUTFChars(path, 0);
char retPath[20];
FILE* fp = fopen(jnamestr,"w+");
if(fp)
{
return env->NewStringUTF(jnamestr);
}
else
{
return env->NewStringUTF("Error opening file!");
}
}
Please help me figure out why this isn't working. Thanks!
请帮我弄清楚为什么这不起作用。谢谢!
1 个解决方案
#1
0
first of all,
首先,
public native String ndkopenfile(String fpath); static{ System.loadLibrary("file-jni"); }
public native String ndkopenfile(String fpath); static {System.loadLibrary(“file-jni”); }
should be within your FileNative class. Second, you'll have to compile your native code as a static library for whatever architectures you want to support (ex: armeabi, armeabi-v7a, x86) and put them in the appropriate directories within the apk.
应该在你的FileNative类中。其次,您必须将您的本机代码编译为您想要支持的任何体系结构的静态库(例如:armeabi,armeabi-v7a,x86),并将它们放在apk中的相应目录中。
You should really read up on the Android NDK and maybe run through a tutorial or two. https://developer.android.com/tools/sdk/ndk/index.html#GetStarted
您应该阅读Android NDK并且可能会通过一两个教程来阅读。 https://developer.android.com/tools/sdk/ndk/index.html#GetStarted
更多相关文章
- android MediaScanner 扫出来的ID3 MP3文件演唱者信息 乱码
- Android的支持库 && app/apk包进系统 && 预置so库进系统 && 预置
- Android Studio Gradle 缓存文件夹设置
- NDK笔记---Android.mk文件
- android设备中的临时文件夹在哪里?
- 使用Java解析XML文件以获取名称列表
- cocos2d-x避免手动修改android.mk文件来编译
- 尝试查看所有XML文件的图形布局时出现StackOverFlowError
- android使用存储在assets文件夹中的Linux工具