3

This question is not related to this question. I have already seen it but it's SPECIFIC Android 4.4.2 issue as the code works fine on other versions of Android.

final File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MY_Downloads");

        if (!path.exists()) {
            path.mkdir(); // also tried it with path.mkdirs();
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(path)));
        }

I am using this code it creates a FILE instead of a folder on Android 4.4.2. Please help I have already read this article

I have given following permissions

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
 <uses-permission android:name="android.permission.WRITE_USER_DICTIONARY" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I know how to access SD-Card and the latest changes regarding public directory but I want to create a FOLDER instead of file

Community
  • 1
  • 1
AZ_
  • 21,688
  • 25
  • 143
  • 191

2 Answers2

1

The first few generations of Android devices always used an SD Card for storage and accessing this was simply a matter of using the path provided by the SDK method

Environment.getExternalStorageDirectory().

Very soon newer devices started to appear with large internal storage partitions which are mapped as external storage such that calls to methods like Environment.

getExternalStoragePublicDirectory(String)

now point to this internal storage rather than a physical SD Card.

Some of the very latest devices such as the Samsung Galaxy Nexus do not even have SD Card slots.

refer this blog

i hope this help.

Piyush Kukadiya
  • 1,880
  • 16
  • 26
  • Dear thanks I just want to create a FOLDER not a file. I have read this and the same code is working with Android 4.3 (internal sd card the same as you are talking about) – AZ_ Mar 05 '14 at 04:00
  • On KitKat both methods return a path to /storage/emulated/0 which is on the internal memory. If I force to create a directory in /storage/external_SD/Android/data//files/Pictures then files is a file on Windows however with ES3 or Wifi File transfer it is a directory. – Zekitez Apr 16 '14 at 16:54
  • I tried the two slashes trick at the end with "/storage/external_SD/Android/data//files/dummy//" and it now creates a directory files which is visible on Windows, but dummy is still a file. Good enough for me :)) http://stackoverflow.com/questions/13953744/file-mkdir-and-mkdirs-are-creating-file-instead-of-directory – Zekitez Apr 16 '14 at 17:45
  • @Zekitez I did the 'dummy' trick too. It was the only way. Too bad I read you comment after hours of tests and found this trick before...You should have made an answer with that as it's an actual solution to the problem. OK, I am doing the answer for the others. – Geoffroy CALA Dec 16 '17 at 11:52
0

I had the same problem on an Android 4.4.2 (and it was not a problem of slash).

For instance if I wanted to created this directory:

myRep/things/logs 

It would create the first 2 directories but 'logs' would be a file, not a directory.

I resolved by adding another directory after logs like:

myRep/things/logs/tmp

Now tmp is a file, but logs is finally a directory.

Also importantly on some devices you need to do additional stuff after creating your file, this is what is working for me:

File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + '/' + pathToDirectory + "/tmp");    
directory.mkdirs();
directory.setExecutable(true);
directory.setReadable(true);
directory.setWritable(true);
MediaScannerConnection.scanFile(context, new String[] {directory .toString()}, null, null);
Geoffroy CALA
  • 931
  • 7
  • 8