import java.io.File;
File folder = new File(Environment.getExternalStorageDirectory() + "/TollCulator");
boolean success = true;
if (!folder.exists()) {
//Toast.makeText(MainActivity.this, "Directory Does Not Exist, Create It", Toast.LENGTH_SHORT).show();
success = folder.mkdir();
}
if (success) {
//Toast.makeText(MainActivity.this, "Directory Created", Toast.LENGTH_SHORT).show();
} else {
//Toast.makeText(MainActivity.this, "Failed - Error", Toast.LENGTH_SHORT).show();
}
上面应该在我的 SD 卡中创建一个文件夹(如果它不存在),如果存在则不执行任何操作。虽然 toast 根据条件工作,但当目录不存在时它不会创建目录。知道如何解决吗?
My Manifest
看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.testing"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="6"
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.test.testing.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>
Update:我更新了我的清单并更新了我的代码,但它仍然没有在我的 SD 卡中创建文件夹。请记住,我使用 Eclipse 并直接在我的手机 (GNex VZW) 上运行应用程序,而不是使用 AVD。
添加此权限Manifest
,
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
File folder = new File(Environment.getExternalStorageDirectory() +
File.separator + "TollCulator");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (success) {
// Do something on success
} else {
// Do something else on failure
}
当你运行应用程序时,也转到DDMS->文件资源管理器->mnt
文件夹->SD卡文件夹->toll-creation文件夹
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)