的Andr​​oid NDK写文件 [英] Android NDK Write File

查看:263
本文介绍了的Andr​​oid NDK写文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有写文件的其他地方比SD卡上的任何其他方式?

我尝试了许多不同的路径上的文件系统,但是FOPEN总是返回NULL,除非我写读/ SD卡/...

有没有别的等价于:

这允许你写这样的文件系统或某事吗?

解决方案
  1. 您可以随时访问该目录 在您的应用程序放置。

    这个目录通常是:

    数据/数据​​/< Your_package_name_usually com.android.appName> /文件/< your_filename>

    但更好的使用 getFilesDir()来 确保与未来有效的文件路径 Android的版本变化。

  2. 如果你想使用外部存储 相反,要确保你把这个 的code。在您的应用程序清单到线 获得许可。

     <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
     

    然后使用SD卡目录:

    getExternalStorageDirectory(); //通常是:/ SD卡/< your_filename>

  3. 否则,您可以用root设备 并获得对整个 文件系统。但你必须要 要求所有的应用程序的用户得到 其设备上的根访问权限之前, 使用你的应用程序。有些游戏 用这样的方式做事,但我 不会推荐它。如果您仍然 想要做到这一点,查找超级用户 应用程序为Android,它是免费的, 值得信赖的。

一旦你拥有了Android目录,只是创建接收的jstring参数JNI本地方法,并设置它在你的本地code。它转换成一个std ::字符串,您就可以使用它与fopen()函数。

  //在你的java的活动
文件F = this.getApplicationContext()getFilesDir()。
LibLoader.setupArchiveDir(f.toString());

//在你的JNI包装
JNIEXPORT布尔JNICALL Java_com_android_appName_LibLoader_setupArchiveDir(JNIEnv的* ENV,jobject OBJ,的jstring DIR)
{
        为const char * TEMP = ENV-> GetStringUTFChars(DIR,NULL);
    标准::字符串stringDir(临时);

        //公司将获得作为一个std :: string的内部C ++ code
    MyNativeObjectInC ++  - > SetupArchiveDir(stringDir);
}
 


编辑:

您可能需要manualy能够使用它的第一次之前创建在C /文件:

INT result_ code =的mkdir(/数据/数据​​/ com.app /文件/,0770)

干杯!

Is there any other way to write a file somewhere else than on the SD card?

I tried many different path on the filesystem but fopen always return NULL, except for any file that I write/read inside the /sdcard/...

Is there something else equivalent to:

That allow you to write like on the file system or something?

解决方案

  1. You can always access the directory in which your app is placed.

    This directory is usually :

    data/data/<Your_package_name_usually com.android.appName>/files/<your_filename>

    but better use getFilesDir() to ensure valid filepath with future version changes of android.

  2. If you wanna use external storage instead, make sure you place this line of code in your app manifest to gain permission.

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    

    Then use the sdcard directory :

    getExternalStorageDirectory(); //usually : /sdcard/<your_filename>

  3. Otherwise, you can root the device and gain access to the whole filesystem. But you'll have to request all of your app users to get root access on their device before using your application. Some games use this way of doing things, but I wouldn't recommend it. If you still wanna do this, lookup Superuser app for android, it's free and trustable.

Once you have the android directory, just create a JNI native method that receive a jstring parameter, and you set it up inside your native code. Convert it into a std::string, and you'll be ready to use it with fopen().

//Inside your java activity
File f = this.getApplicationContext().getFilesDir(); 
LibLoader.setupArchiveDir(f.toString());

//Inside your JNI wrapper
JNIEXPORT bool JNICALL Java_com_android_appName_LibLoader_setupArchiveDir(JNIEnv * env, jobject obj, jstring dir)
{
        const char* temp = env->GetStringUTFChars(dir, NULL);
    std::string stringDir(temp);

        // Will be receive as a std::string inside C++ code 
    MyNativeObjectInC++->SetupArchiveDir(stringDir);
}


Edit :

You might need to manualy creates the /files in C before being able to use it on the first time :

int result_code = mkdir("/data/data/com.app/files/", 0770)

Cheers !

这篇关于的Andr​​oid NDK写文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆