2

how can i create a pre-allocated file on android? something like a sparse file ?

i need to make an applicaton that will download a large file , and i want to avoid having an out-of-space error while the download is commencing . my solution needs to support resuming and writing to both internal and external storage.

i've tried what is written here: Create file with given size in Java but for some reason, none of those solutions worked.

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270

1 Answers1

3

this is odd. it works fine now . perhaps i've tested it in a wrong way.

here's a working code , for those who have problems with it :

final String path=getFilesDir().getParent()+"/t.txt";
RandomAccessFile newSparseFile=null;
try
  {
  new File(path).delete();
  newSparseFile=new RandomAccessFile(path,"rw");
  // create a 1MB file:
  newSparseFile.setLength(1024*1024);
  }
catch(final Exception e)
  {
  Log.e("DEBUG","error while creating file:"+e);
  }
finally
  {
  if(newSparseFile!=null)
    try
      {
      newSparseFile.close();
      Log.d("DEBUG","length:"+new File(path).length());
      }
    catch(final IOException e)
      {
      Log.e("DEBUG","error while closing file:"+e);
      }
  }
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Seems unreliable at best, need a check to see if setLength worked, I find the size will remain 0 if there isn't enough space, or it will just be smaller than expected but not taking up all the disc space. – Nathan Schwermann Oct 25 '13 at 17:43
  • So have you found that it is at all reliable or is it completely useless? Any tests I've done haven't been promising... – Nathan Schwermann Oct 25 '13 at 20:58
  • i haven't used it recently. you are saying it doesn't work at all? – android developer Oct 26 '13 at 08:59
  • I haven't tested much, it seems to work with small file sizes. I tried using really big longs to make a few gigabyte sized file and instead it just did something like 400 bytes or 0 bytes. – Nathan Schwermann Oct 26 '13 at 16:13
  • 1
    @schwiz i've now tried it and it worked fine even with 1GB. i've updated the code to show that it's correct. if it doesn't work for you, maybe it works in a different way on different devices. maybe you could just jump in the file to the last byte and write something there, and go back. for me, the code i've written here works fine on galaxy S3 with Android 4.1.2 . for example, if i set the size to be 1GB (1024*1024*1024), it gets to be this size . i can even see the file size using the "total commander" app. – android developer Oct 26 '13 at 21:50
  • i have this all function just do it in ur mob and observe free or available free space in ur mobile it will not effected after creating this 1GB file dude – Bhanu Sharma Jan 16 '14 at 11:37
  • means if u see that file properties in sdcard then it is 1 GB but free space is not effected.... Y so plz help – Bhanu Sharma Jan 16 '14 at 11:41
  • @BhanuSharma have you tried writing to the end of the file as i've suggested? – android developer Jan 16 '14 at 12:20
  • yes, first time that one is blank then size is 1 GB but free space is not effected then i wrote something in that file like "abcd" from file.write(data.getBytes()); then that string is write but file size is also 1 GB actually i want to create blank file with some file size which decrease total free space see my question once ... http://stackoverflow.com/questions/21157058/create-jpeg-txt-any-type-file-which-requires-to-be-fixed-size-which-user-will-gi/21157368?noredirect=1#comment31850674_21157368 – Bhanu Sharma Jan 16 '14 at 12:33
  • @BhanuSharma maybe on some devices the implementation is different? maybe the size left isn't affected by using this operation? weird... – android developer Jan 16 '14 at 12:37
  • u have something like i create one blank file with 1 Gb size and memory is also affected and later i write 1 GB content in that file plzz help – Bhanu Sharma Jan 16 '14 at 12:47
  • not currently. sorry. hope i will remember to check it out at home. – android developer Jan 16 '14 at 13:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45431/discussion-between-bhanu-sharma-and-android-developer) – Bhanu Sharma Jan 17 '14 at 06:05