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

更多相关文章

  1. android MediaScanner 扫出来的ID3 MP3文件演唱者信息 乱码
  2. Android的支持库 && app/apk包进系统 && 预置so库进系统 && 预置
  3. Android Studio Gradle 缓存文件夹设置
  4. NDK笔记---Android.mk文件
  5. android设备中的临时文件夹在哪里?
  6. 使用Java解析XML文件以获取名称列表
  7. cocos2d-x避免手动修改android.mk文件来编译
  8. 尝试查看所有XML文件的图形布局时出现StackOverFlowError
  9. android使用存储在assets文件夹中的Linux工具

随机推荐

  1. 基于fastai的分类网络
  2. 关于Python的编码注释# -*- coding:utf-8
  3. Python列表以及列表的处理方法
  4. AttributeError:“MatrixFactorizationMo
  5. 小白学Python---面向对象02
  6. Spark学习,pyspark执行后默认启动Ipython
  7. 通过分隔符计数和位置从数据框中提取特定
  8. 安装python2.6.6到ubuntu12.04
  9. Java程序不像python程序那样工作,我不知道
  10. python中list的拷贝与numpy的array的拷贝