使用Android NDK将文件写入SD卡以外的位置? [英] Write file to location other than SDcard using Android NDK?

查看:17
本文介绍了使用Android NDK将文件写入SD卡以外的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了在 SD 卡上之外,还有其他方法可以将文件写入其他地方吗?

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

我在文件系统上尝试了许多不同的路径,但 fopen 总是返回 NULL,除了我在/sdcard/中写入/读取的任何文件...

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

还有其他相当于:

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

那可以让你像在文件系统上写什么?

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

推荐答案

  1. 您可以随时访问该目录您的应用所在的位置.

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

这个目录通常是:

data/data/

但最好使用 getFilesDir() 来确保具有未来的有效文件路径android的版本变化.

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

如果您想使用外部存储相反,请确保将其放置应用清单中的代码行获得许可.

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"/>

然后使用sdcard目录:

Then use the sdcard directory :

getExternalStorageDirectory();//通常:/sdcard/<your_filename>

否则,你可以root设备并获得对整体的访问权文件系统.但你必须请求您的所有应用用户获取之前在他们的设备上进行 root 访问使用您的应用程序.一些游戏用这种方式做事,但我不会推荐它.如果你还想要这样做,查找 Superuser安卓应用程序,它是免费的值得信赖.

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.

一旦你有了 android 目录,只需创建一个接收 jstring 参数的 JNI 本地方法,然后在你的本地代码中设置它.将其转换为 std::string,您就可以将其与 fopen() 一起使用.

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);
}

<小时>

您可能需要在 C 中手动创建/files 才能第一次使用它:

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)

干杯!

这篇关于使用Android NDK将文件写入SD卡以外的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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