我无法弄清楚创建新文件时出了什么问题,我已经测试了以下代码并且工作正常,但在当前项目中我无法创建新文件。
创建文件:
public static File createFile() {
final String SDCARD = Environment.getExternalStorageDirectory().getPath();
final String TEMP_FOLDER = SDCARD + File.separator + "AVL1" + File.separator + "temp";
final String name = "myfile3.png";
final File directory = new File(TEMP_FOLDER);
File tst = null;
try {
tst = new File(TEMP_FOLDER, name);
if (tst.exists())
return tst;
if (directory.exists()) {
Log.i("fdbg", "directory exists");
tst.createNewFile();
} else {
Log.i("fdbg", "directory not exists");
directory.mkdirs();
tst.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
return tst;
}
例外:
10-04 16:21:02.995 9533-9533/app.name W/System.err: java.io.IOException: open failed: EACCES (Permission denied)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at java.io.File.createNewFile(File.java:941)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at app.name.Views.Activities.MainActivity.createTempFile(MainActivity.java:290)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at app.name.Views.Activities.MainActivity.onCreate(MainActivity.java:149)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at android.app.Activity.performCreate(Activity.java:6221)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at android.app.ActivityThread.access$900(ActivityThread.java:172)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at android.os.Looper.loop(Looper.java:145)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5832)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at java.lang.reflect.Method.invoke(Native Method)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
10-04 16:21:02.995 9533-9533/app.name W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at libcore.io.Posix.open(Native Method)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
10-04 16:21:02.995 9533-9533/app.name W/System.err: at java.io.File.createNewFile(File.java:934)
10-04 16:21:02.995 9533-9533/app.name W/System.err: ... 15 more
App build.gradle
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "package.name"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
renderscriptTargetApi 21
renderscriptSupportModeEnabled true
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
//some repos
}
dependencies {
//project dependencies
}
apply plugin: 'com.google.gms.google-services'
清单权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
/>
设备API = 21
答案 0 :(得分:0)
我正在测试这是在Android 6.0上,如果是,那么你必须添加一些代码来授予这个权限