小编典典

在Android中创建文件夹

java

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卡中创建一个文件夹(如果不存在),则不要执行任何操作。尽管吐司根据条件起作用,但是当不存在时不会创建目录。知道如何解决吗?

我的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>

更新 :我更新了清单以及代码,但仍未在SD卡中创建文件夹。请记住,我正在使用Eclipse,并且直接在手机(GNex
VZW)上运行该应用程序,而不是使用AVD。


阅读 1598

收藏
2020-09-08

共1个答案

小编典典

在中添加此权限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文件夹-> sdcard文件夹->收费创建文件夹

2020-09-08