创建临时文件中的Android NDK用 [英] Creating temporary files in Android with NDK
问题描述
我目前工作的一个基于C语言的基于NDK-,Android应用程序。此应用程序需要创建临时文件。定期Linux系统上,我会使用 TMPFILE
,以确保这些文件在临时目录中正确地创建和清理后的在过程结束。
I am currently working on a C-based, NDK-based, Android application. This application needs to create temporary files. On a regular Linux system, I would use tmpfile
to ensure that these files are properly created in a temporary directory and cleaned-up at process end.
不过,我在各种Android设备的研究似乎表明,
However, my investigations on various Android devices seem to indicate that
-
TMPFILE
总是失败; - 没有
/ tmp目录
目录; - 目录
/数据/本地/ tmp目录
不是在Android上的所有变体present; - 没有
TEMP
环境变量设置; -
mkstemp
不工作比任何TMPFILE
更好。
tmpfile
always fails;- there is no
/tmp
directory; - directory
/data/local/tmp
isn't present on all variants of Android; - there is no
TEMP
environment variable set; mkstemp
doesn't work any better thantmpfile
.
现在,我敢肯定,我可以砍的东西在一起,但看到该SDK提供了 context.getCacheDir
和 File.createTempFile
Java应用程序,我希望有一个在C级等同。
Now, I'm sure that I could hack something together, but seeing that the SDK offers context.getCacheDir
and File.createTempFile
for Java applications, I hope that there is an equivalent at C-level.
有谁知道一个良好的可靠的和交叉的Android方法创建一个临时文件?
Does anyone know of a good reliable and cross-Android method for creating a temporary file?
推荐答案
我们已经找到了最好的方法是调用 Context.getCacheDir
启动时,用得到它的路径 getAbsolutePath
,然后调用一个JNI函数该路径存储在一个全球性的。想要创建一个临时文件中的任何功能,只是附加了一个合适的临时文件名的路径。
The best way we've found is to call Context.getCacheDir
on startup, get its path with getAbsolutePath
, then call a JNI function to store that path in a global. Any function that wants to create a temporary file simply appends a suitable temporary file name to that path.
如果你真的想从JNI去取另一种方法是在上下文
来传递给JNI的功能和使用一堆的GetMethodID的
/ CallObjectMethod
东西回调到Java来 getCacheDir
,但前者的做法是简单得多。
If you really want to fetch it from JNI another alternative would be to pass in a Context
to a JNI function and use a bunch of GetMethodID
/ CallObjectMethod
stuff to call back into Java to getCacheDir
, but the former approach is a lot simpler.
不幸的是,似乎没有要在此刻更优雅的解决方案。
Unfortunately, there does not appear to be a more elegant solution at the moment.
这篇关于创建临时文件中的Android NDK用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!