Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble Creating a Directory on the SD Card
    primarykey
    data
    text
    <p>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)</p> <pre><code>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 } } } </code></pre> <p>Here is my Manifest file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="soapbox.sym3try.andguard" android:versionCode="1" android:versionName="1.0"&gt; &lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt; &lt;activity android:name=".Main" android:label="@string/app_name"&gt; &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"&gt;&lt;/uses-permission&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;uses-sdk android:minSdkVersion="4" /&gt; &lt;/manifest&gt; </code></pre> <p>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:</p> <pre><code> 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload