目录确实存在且不存在

时间:2013-04-05 17:01:54

标签: android

我正在尝试将文件写入我的Android手机上的SD卡,但它似乎与目录有问题。它存在但不存在。我的意思是当我去Android手机上的文件浏览器,并找到GPStracker文件夹,我找不到它。然而,在程序中,它似乎承认在检查下面的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;
}

^这些现在都返回true,修复了这个问题。

清单:

<?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>

代码:

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();
        }

0 个答案:

没有答案