问题是基本的,但它仍然让我疯狂:如何创建一个简单的文本文件到公开可见的目录,比如下载文件夹?我正在使用的android使用Android版本6.0.1,而应用程序本身的最低级别为20级。
我没有SD卡,但据我所知,内部和外部这两个术语指的是目录的可见性和访问权限:如果只有应用程序看到该文件,那么内部。如果用户看到该文件,则它是外部的。
首先,我在AnrdoidManifext.xml中有必需的uses-permission标记,尽管在这个问题的上下文中我不需要读取权限。
configure:3243: checking whether the C compiler works
configure:3265: /home/ubuntu/android-ndk-r12b/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-gcc --sysroot=/home/ubuntu/android-ndk-r12b/platforms/android-16/arch-arm/ -O2 --sysroot=/home/ubuntu/android-ndk-r12b/platforms/android-16/arch-arm/ conftest.c >&5
/home/ubuntu/android-ndk-r12b/platforms/android-16/arch-arm/usr/lib/crtbegin_dynamic.o: error adding symbols: File in wrong format
collect2: error: ld returned 1 exit status
configure:3269: $? = 1
configure:3307: result: no
configure: failed program was:
| /* confdefs.h */
| #define PACKAGE_NAME "fdk-aac"
| #define PACKAGE_TARNAME "fdk-aac"
| #define PACKAGE_VERSION "0.1.4"
| #define PACKAGE_STRING "fdk-aac 0.1.4"
| #define PACKAGE_BUGREPORT "http://sourceforge.net/projects/opencore-amr/"
| #define PACKAGE_URL ""
| #define PACKAGE "fdk-aac"
| #define VERSION "0.1.4"
| /* end confdefs.h. */
|
| int
| main ()
| {
|
| ;
| return 0;
| }
configure:3312: error: in `/home/ubuntu/fdk-aac-0.1.4':
configure:3314: error: C compiler cannot create executables
然后是代码本身。您可以从调试日志中了解到我对此感到有点沮丧。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mylittleapp">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application>
<!-- Not relevant... right? -->
</application>
</manifest>
此处,程序在public class LogFileWriter {
File mFile;
FileOutputStream mStream;
LogFileWriter(String filename) {
// Check if storage is available
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {
Log.d("LogFileWriter", "I'm not your state buddy!");
return;
}
File target = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
if (!target.exists() && !target.mkdir()) {
Log.d("LogFileWriter", "I'm not your folder guy!");
return
}
mFile = new File(target, filename);
try {
if (!mFile.exists())
Log.d("LogFileWriter", "I'm not your file friend!");
else {
mStream = new FileOutputStream(mFile);
}
} catch (FileNotFoundException e) {
Log.d("LogFileWriter", "Exceptional!");
}
}
检查失败并打印!mFile.exist()
如果我要添加I'm not your file friend!
,那将导致IOException ......
mFile.createNewFile()
尽管程序应该可以访问公共目录!
我还尝试将文件写入java.io.IOException: open failed: EACCES (Permission denied)
目录,但结果相同:我仍然没有写入权限。
我还检查了Context.getExternalFilesDir()
导致的位置。它的路径是Environment.getExternalStoragePublicDirectory
。
如何获得外部存储的写入权限?有什么想法吗?
答案 0 :(得分:1)
正如@SaravInfern和其他人指出的那样,问题是在最新版本的android中我还必须在运行时请求写权限。谢谢你们!
https://developer.android.com/training/permissions/requesting.html