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

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

问题描述

除了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/<Your_package_name_usually com.android.appName>/files/<your_filename>

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

,但最好使用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(); //usually : /sdcard/<your_filename>

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

否则,您可以启动设备 并获得整体访问权 文件系统.但是你必须 要求您所有的应用程序用户获取 之前在其设备上进行root访问 使用您的应用程序.一些游戏 用这种方式做事,但是我 不会推荐它.如果你仍然 要执行此操作,查找Superuser 适用于android的应用程序,它是免费的 值得信赖.

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中手动创建/file,然后才能首次使用它:

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将文件写入SDcard以外的位置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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