Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android mkdirs() creates a zero byte file instead of a folder

In my android application, I am trying to create the following folder on the sdcard:

/mnt/sdcard/OSGiComponents/admin/felix-cache/

Here's the code:

File cacheDir = 
    new File( Environment.getExternalStorageDirectory().getAbsolutePath() + 
              "/OSGiComponents/admin/felix-cache/" );
// Create the folder
cacheDir.mkdirs();
// Check if it exists
if ( ! cacheDir.exists() ) {
    Log.e ( "Debug" , "Cache directory cannot be created" );
}

I have the WRITE_STORAGE_PERMISSION under the manifest tag of the android manifest file. I am able to create other folders and files without problem on the sdcard. The app works fine on the following phones:

  1. Nexus S (rooted) running Gingerbread (2.3)
  2. Nexus S (unrooted) running Jelly Bean (4.1.2)
  3. HTC Desire (rooted) running Froyo (2.2)
  4. HTC Desire (unrooted) running Froyo (2.2)

However on Samsung Galaxy Nexus phone (unrooted) running Ice Cream Sandwich (4.0.4), the directory is created as a zero size file, which can be seen in Astro. The exists() call returns false.

Astro showing a zero byte felix-cache file

  1. As you can see from the folder name, I am using Apache Felix. Felix creates the cache directory automatically if it does not exist. On Galaxy Nexus, it always complained that it is unable to create the cache directory. Astro shows a 0 byte file instead of a folder. This is why I decided to try creating the cache folder myself before initializing Felix.
  2. So, I create the cache folder myself. The app works fine the first time, and I can see the folder fine in Astro. If I close the app, then delete the folder in Astro, and then re-launch the app, even my code mysteriously cannot create the cache directory, and Astro shows a 0 byte file.
  3. The 0 byte file cannot be deleted in Astro. However, when I reboot the phone, the folder is magically there and ok.
  4. I use FileInstall to watch the OSGiComponents/install folder. When I drop bundle jars into that folder, it is detected and installed ok on all phones except Galaxy Nexus (when the app works the first time). There are no logs/errors from FileInstall about not being able to watch the directory.
  5. I have tested this on 2 Galaxy Nexus phones, same problem.

I suspect it is a permissions problem, but I not sure what it is, and why a 0 byte file is created while exists() returns false. Nowhere else in the code am I creating this file.

Any suggestions on what could be the problem?

Thanks :)

UPDATE: I think I have identified the issue, please see the answer I posted.

like image 991
Kartik Sankaran Avatar asked Dec 19 '12 09:12

Kartik Sankaran


People also ask

Why is a file downloading as 0 bytes?

Zero byte file may be caused by Incompletely file downloading via Web or file transfer protocol client, or incorrectly transmitting email attachment. Save the file with empty content. Corrupted index table in the file system. Computer cannot handle files or folders with extremely long name.

How do I get rid of zero-byte file?

From the Empty Files tab, click Mark all Files and then click Delete Files. Similarly, to delete the 0-byte files in the selected folder tree, click on the Empty Files tab.

Can a file have 0 bytes?

A zero-byte file is a file that does not contain any data. While most files contain several bytes, kilobytes (thousands of bytes) or megabytes (millions of bytes) of information, the aptly-named zero-byte file contains zero bytes. Usually a file will contain at least a few bytes.


1 Answers

please use instead of

   File cacheDir = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + 
          "/OSGiComponents/admin/felix-cache/" );
      cacheDir.mkdirs();

to

  File cacheDir = 
new File( Environment.getExternalStorageDirectory().getAbsolutePath() + 
          "/OSGiComponents/admin/felix-cache" );
     cacheDir.mkdir();
like image 132
raju Avatar answered Oct 05 '22 20:10

raju