1

CONFUSED: Those who down vote a question, could you add some comments, why???? I want to say bad words, just dont want to get banned.

I do have an app and I want to have a folder in sdCard so users can add some stuff and then i can use it inside the app. I use the following code to create a folder:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //External storage
        boolean mExternalStorageAvailable = false;
        boolean mExternalStorageWriteable = false;
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // We can read and write the media
            mExternalStorageAvailable = mExternalStorageWriteable = true;
            Log.v("Storage","ablo to read and write");
            //create Beatss folder
            File direct = new File(android.os.Environment.getExternalStorageDirectory().getPath() + "/Beatss");
            Log.v("Storage",android.os.Environment.getExternalStorageDirectory().getPath() + "/Beatss");
                boolean success = true;
                if (!direct.exists()) {
                    success = direct.mkdirs();

                }
                if (success) {
                    // Do something on success
                    Log.v("Storage","Folder has been created");

                } else {
                    // Do something else on failure 

                }

        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            // We can only read the media
            mExternalStorageAvailable = true;
            mExternalStorageWriteable = false;
            Log.v("Storage","ablo to read only");
        } else {

            mExternalStorageAvailable = mExternalStorageWriteable = false;
            Log.v("Storage","no access");               
        }

I can see in DDMS that folder has been created but when i go through WindowsExplorer I have a file instead of a folder. So, how to create a folder?

UPD1: So, even when i create something inside this folder, i still have it as a file inside the windows explorer, but inside the DDMS i can see that i have a folder and inside it another folder. So, what is wrong here??

UPD2: So, breaked up direct line in logcat to see if there is a real path:

01-24 21:14:04.368: V/Storage(12116): /mnt/sdcard/Beatss/

So there is..

UPD3: I cant show all my code it is 264 lines so far. All this folder creating happens at onCreate of mainActivity. The manifestFile looks like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uk.test
android:versionCode="1"
android:versionName="1.0"
android:installLocation="auto" >

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="16" />
<supports-screens         
              android:smallScreens= "true"
              android:normalScreens="true"
              android:largeScreens="true"
              android:xlargeScreens="false"                  
/>


    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:screenOrientation="portrait"
    >
    <activity
        android:name=".Main"
        android:label="@string/app_name"
        android:screenOrientation="portrait" 
        android:clearTaskOnLaunch="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.google.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>        
    <activity android:name=".MainActivity"
              android:label="@string/app_name"
              android:screenOrientation="portrait">
    </activity>
    <activity android:name=".SupportScreen"
              android:label="@string/app_name"
              android:screenOrientation="portrait">
    </activity>
        <activity android:name="com.tapjoy.TJCOffersWebView" android:configChanges="keyboardHidden|orientation" />
        <activity android:name="com.tapjoy.TapjoyFullScreenAdWebView" android:configChanges="keyboardHidden|orientation" />
        <activity android:name="com.tapjoy.TapjoyDailyRewardAdWebView" android:configChanges="keyboardHidden|orientation" />
        <activity android:name="com.tapjoy.TapjoyVideoView" android:configChanges="keyboardHidden|orientation" />

</application>
</manifest>

Update code upper:

Logcat says:

01-24 21:19:21.068: V/Storage(14088): /mnt/sdcard/Beatss/
01-24 21:19:21.138: V/Storage(14088): Folder has been created
Daler
  • 1,205
  • 3
  • 18
  • 39
  • What happens, if you create files inside this "folder" or when you check `direct.isDirectory()` afterwards? ;) – ConcurrentHashMap Jan 24 '13 at 15:56
  • 1
    never tried so far... interesting – Daler Jan 24 '13 at 15:57
  • 3
    Just a cautionary note - do not p-off users who find their sdcard with dozen of directories all over the sdcard - make it a habit to store in `/sdcard/Android/my.pkg.name/.....` to make it easier for the end user to clean up, should the need arise... ;) – t0mm13b Jan 24 '13 at 16:02
  • yeah yeah yeah, first lets make to create that folder after can think about managing – Daler Jan 25 '13 at 05:17
  • Seems like similar issue to this [question](http://stackoverflow.com/questions/7429087/cant-see-a-file-in-windows-written-by-an-android-app-on-sd-card-unless-i-force). – AggelosK Jan 28 '13 at 13:33
  • You should use getExternalStorageDirectory and then File file = new File(yourThisDirectory); `File file = new File(directory); file.mkdirs(); File outputFile = new File(file, "your file name"); FileOutputStream f = new FileOutputStream(outputFile);` – techieWings Feb 02 '13 at 12:50

4 Answers4

2

Try it

String PATH = Environment.getExternalStorageDirectory()+"/Folder123/";
            File file = new File(PATH);
            file.mkdirs();
Gabriel Augusto
  • 1,002
  • 11
  • 18
  • it creates a folder but i cant see it via WindowsExplorer. The same situation that i have right now, so not helpful – Daler Jan 24 '13 at 16:55
  • I added working code as an answer that used the same technique as the comment above. This code should work because it is using the same technique as the working code I posted. Be aware that there are two SD cards on devices that have removable SD cards. Windows Explorer will display two directories with names something like Device and Card. getExternalStorageDirectory() returns the device and not the removable SD card directory. – Howard Hodson Jan 24 '13 at 21:56
  • Thanks.. you have another method? – Gabriel Augusto Jan 30 '13 at 17:21
0

Here's a code snippet from an app I have in production....

if (spaceAvailable > spaceRequired) {
        databasePath = new File(settingsDatabasePath, GC.APP_DIRECTORY);
        databasePath.mkdirs();
        settingsDatabasePath = databasePath.getPath().toString();
    }

settingsDatabasePath is determined when the app is first installed. It can be either external or internal storage depending on whether the device has external storage or not. Also, prior to this snippet, it has been determined that the GC.APP_DIRECTORY (a string constant in the GC class) has not been created yet. Also, after this snippet, tests are made to ensure that the directory has been created, that it is writable, and that it is a directory. Databases and files are created in the app directory to keep from 'splattering' files in the root level directory.

Note: settingsDatabasePath is being deliberately over written. If you copy the code, you may not want to do the over write.

Howard Hodson
  • 943
  • 7
  • 17
  • you actually put half of something, what is useful in this code? where is the beginning? I've tired a lot of ways creating that god damn folder but it creates a file instead of folder. – Daler Jan 25 '13 at 04:50
  • The key code is duplicates gabrielaugustodm's. Using mkdirs() creates the entire string of directories. mkdir() only creates the last directory on an existing directory tree. Both will return a false if they fail. Neither will create a file. Your problem appears to be trying to create a directory with the same name as an existing file at the level the directory should be. Or, you are creating the structure on the non-removable (system) sd card. Basically, I'm saying that the file.mkdirs() process is correct. You are having problems with name collisions or working with two sd cards, not one. – Howard Hodson Jan 28 '13 at 17:09
  • the problem is, that all your codes that you posted is also the same as I did poste in my question. It does create a folder, inside the android system u can work with it without any problem. I cant see it as a folder inside the WindowsExplorer and this is the issue, not creating a folder. How to make it work as a folder – Daler Jan 29 '13 at 12:13
  • Okay, so you are creating a directory. How are you trying to create files within the directory? – Howard Hodson Jan 29 '13 at 14:41
  • I just want to create an empty set of directories, so user will fill it with data from their computers. – Daler Jan 29 '13 at 15:22
  • Try deleting everything you've created with Windows Explorer (fresh start). After you do the 'if success' and it's true, add a test to see if it is a directory. Log the results. Going in the first time, you should get the false for the exists test and a true for the success and isDirectory tests. You write the code a bit differently than I would, but I don't see why it shouldn't work the first time it runs. You might want to put a few more log statements in the code and see what is happening in more detail. – Howard Hodson Feb 08 '13 at 18:38
0

try this :

String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File folder = new File(extStorageDirectory, "DIR_NAME");
    folder.mkdir();
    File file = new File(folder, "Sample.txt");
    try {
        file.createNewFile();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
Anand
  • 2,875
  • 1
  • 18
  • 16
0

There is nothing wrong with your code. I use this same code on all my apps and they work just fine. I copy/pasted your code and it works great. The folder "Beatss" is viewable on my Windows machine. You MUST have an issue with your sd card. You either need to check it for errors or try another sd card.

In Windows right click on the sdcard then select properties>tools>error-checking then click 'check now'. If there are no errors then try another card. If not that, then try your apk on another device alltogether and see if it works there.

MrBruce
  • 21
  • 1
  • i tried in two phones, dont work so.. not the flash card. I have to try it on some other manufacturer maybe, all my test devices are from Sony. – Daler Feb 01 '13 at 13:22