无法在SD卡上创建目录

时间:2010-10-04 00:48:14

标签: android

我正在尝试让我的应用在SD卡的根目录上创建一个目录,如果该目录尚不存在的话。当我运行应用程序时,显示“正在创建主目录...”吐司通知,但目录未创建...我做错了什么? (P.S.权限设置为写入外部存储器)

public class Main extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

     // Check SD Card for Read/Write

            boolean mExternalStorageWriteable = false;
            String state = Environment.getExternalStorageState();

            if (Environment.MEDIA_MOUNTED.equals(state)) {
                // We can read and write the media
                mExternalStorageWriteable = true;
            } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
                // We can only read the media
                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
                mExternalStorageWriteable = false;
            }

            if (mExternalStorageWriteable == false) {
            //Toast Notification that SD Card is not Accessible 
             Context context = getApplicationContext();
             CharSequence text = "SD Card is NOT Accessable, Please Remount and Try Again";
             int duration = Toast.LENGTH_SHORT;

             Toast toast = Toast.makeText(context, text, duration);
             toast.show();
            } else {
             //Check If Home Dir Exists

             boolean exists = (new File("/AndGuard/")).exists();
             if (exists) {
              Context context = getApplicationContext();
                 CharSequence text = "Accessing Data Files...";
                 int duration = Toast.LENGTH_SHORT;

                 Toast toast = Toast.makeText(context, text, duration);
                 toast.show();

             } else {
              Context context = getApplicationContext();
                 CharSequence text = "Creating Home Directory...";
                 int duration = Toast.LENGTH_SHORT;

                 Toast toast = Toast.makeText(context, text, duration);
                 toast.show();

            // Create a directory; all non-existent ancestor directories are
         // automatically created

           // Create a directory; all non-existent ancestor directories are
         // automatically created
         File root = Environment.getExternalStorageDirectory();
         boolean success = (new File(root,"directoryName")).mkdirs();
         if (!success) {
             // Directory creation failed
         }
             }         
            }

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="soapbox.sym3try.andguard"
      android:versionCode="1"
      android:versionName="1.0">
          <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Main"
                  android:label="@string/app_name">
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="4" />


</manifest> 

我也尝试了这个,它没有用,但它可能是朝着正确方向迈出的一步,因为它创建了目录并用res / raw中的文件一步一步填充它:

 public boolean saveas(int ressound){
         byte[] buffer=null;
         InputStream fIn = getBaseContext().getResources().openRawResource(ressound);
         int size=0;

         try {
          size = fIn.available();
          buffer = new byte[size];
          fIn.read(buffer);
          fIn.close();
         } catch (IOException e) {
          // TODO Auto-generated catch block
          return false;
         }

         String path="/sdcard/AndGuard/List1";
         String filename="hosts";

         boolean exists = (new File(path)).exists();
         if (!exists){new File(path).mkdirs();}

         FileOutputStream save;
         try {
          save = new FileOutputStream(path+filename);
          save.write(buffer);
          save.flush();
          save.close();
         } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          return false;
         } catch (IOException e) {
          // TODO Auto-generated catch block
          return false;
         }
        return exists;    
}

4 个答案:

答案 0 :(得分:1)

我认为,uses-permission应该在manifest xml标签中,请查看http://developer.android.com/guide/topics/manifest/uses-permission-element.html

答案 1 :(得分:0)

这可能无法执行任何操作,但在您的清单中的WRITE_EXTERNAL_STORAGE之后添加此权限

android:name="android.permission.READ_PHONE_STATE"

答案 2 :(得分:0)

//create file dir
File    wallpaperDirectory = new File("/sdcard/example");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();

这对我有用。您需要设置

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">

答案 3 :(得分:0)

uses-permission应该在manifest标签中,而不是在activity中。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="soapbox.sym3try.andguard"
      android:versionCode="1"
      android:versionName="1.0">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-sdk android:minSdkVersion="4" />


    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name">

        <activity
            android:name=".Main"
            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> 

你不能在根目录中创建任何文件,所以我认为它必须是我

File root = Environment.getExternalStorageDirectory();
boolean exists = (new File(root, "AndGuard")).exists();

硬编码/ SD卡/错误。

String path="/sdcard/AndGuard/List1"

File path = new File(root, "AndGuard/List1");

save = new FileOutputStream(path+filename);

会给你

"/sdcard/AndGuard/List1hosts"

所以一定是

save = new FileOutputStream(new File(path, filename));