开发者

Trouble Creating a Directory on the SD Card

I am trying to have my app create a directory on the root of the SD Card, if that directory does not already exist. When I run the app, the "Creating Home Directory..." toast notification displays, but the directory is not created... What am i doing wrong?? (P.S. permissions are set for writing to external storage)

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

Here is my Manifest file:

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

I tried this as well, it didn't work, but it might be a step in the right direction as it creates the directory and populates it with a file from res/raw all in one step:

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


The uses-permission should be in the manifest xml tag I think, check out http://developer.android.com/guide/topics/manifest/uses-permission-element.html


This may not do anything, but right after WRITE_EXTERNAL_STORAGE" in your manifest add this permission

android:name="android.permission.READ_PHONE_STATE"


The uses-permission should be in manifest tag, not in 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> 

You can't create any files in the root directory, so I assume it must me

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

Hardcoding /sdcard/ is wrong.

String path="/sdcard/AndGuard/List1"

right

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

And

save = new FileOutputStream(path+filename);

will give you

"/sdcard/AndGuard/List1hosts"

So it must be

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


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

This works for me. You will need to set

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜