Android的 - $ P $下载前一个文件p分配空间 [英] android - pre-allocate space for a file before downloading it

查看:197
本文介绍了Android的 - $ P $下载前一个文件p分配空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建Android上pre-分配的文件?像稀疏文件?

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.

我试过什么写在这里:
在Java 给定大小创建文件
但由于某些原因,没有这些解决方案的工作。

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

推荐答案

这是奇怪的。
现在工作正常。也许我在一个错误的方式进行了测试。

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

这里的工作code,对于那些谁的问题吧:

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的 - $ P $下载前一个文件p分配空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆