Android 目录执行和不执行';不存在
我正试图在我的android手机上的SD卡上写一个文件,但它的目录似乎有问题。它存在但不存在。我的意思是,当我在安卓手机上打开文件浏览器,查找GPStracker文件夹时,我找不到它。然而,在程序中,当它检查下面的Android 目录执行和不执行';不存在,android,Android,我正试图在我的android手机上的SD卡上写一个文件,但它的目录似乎有问题。它存在但不存在。我的意思是,当我在安卓手机上打开文件浏览器,查找GPStracker文件夹时,我找不到它。然而,在程序中,当它检查下面的dir.exists()if条件时,它似乎承认该目录存在。然后,当它到达canWrite()函数时,它总是返回false。我不知道这一点出了什么问题,有人有任何调试想法吗?谢谢 boolean mExternalStorageAvailable = false; boolean mEx
dir.exists()
if条件时,它似乎承认该目录存在。然后,当它到达canWrite()
函数时,它总是返回false。我不知道这一点出了什么问题,有人有任何调试想法吗?谢谢
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
^现在这两个都返回真值,解决了这个问题
舱单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tinywebteam.gpstracker"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.tinywebteam.gpstracker.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
您的代码没有说它存在。。如果目录不存在,它将尝试创建该目录。此时,如果(!dir.exists())这不是说如果目录不存在,就创建它吗?如果它跳过了这段代码,它现在不应该存在吗?此时,当条件的计算结果为false时,它总是跳过这段代码。然后直接进入文件输出行。好的,这和你的文件有关。它无法写入文件,可能是因为名称无效?>什么是
ts
?请尝试新建文件(SD卡,“test.csv”)代码>排除它是一个目录/命名problem@Doomsknightts是一个长时间戳。我只是在没有目录的情况下进行了尝试,但仍然无法满足canWrite()
条件。SD卡是否可能采用了某种错误的格式?
try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard + "/GPStracker/data");
if (!dir.exists()) {
if (!dir.mkdirs())
throw new FileNotFoundException("Couldn't make directory.");
if (!dir.isDirectory())
throw new FileNotFoundException("Couldn't verify directory.");
}
File fileOut = new File(dir, "GPSTracker_" + ts + ".csv");
if (fileOut.canWrite()) {
FileOutputStream out = new FileOutputStream(fileOut);
out.write(fileStr.getBytes());
out.close();
System.out.println("File written successfully");
} else {
System.out.println("Could not write to file");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}