简体   繁体   English

使用存储访问框架

[英]using Storage Access Framework

It works fine if you write a few data. 如果您写入一些数据,它将很好地工作。 But if you write a lot of data (which will run for a long time), it will fail with: java.io.IOException: write failed: EBADF (Bad file number) 但是,如果您写入大量数据(将运行很长时间),它将失败并显示以下内容: java.io.IOException: write failed: EBADF (Bad file number)

Here's the code: 这是代码:

writeLargeDataToStream(new FileOutputStream(getContentResolver()
        .openFileDescriptor(data.getData(), "w").getFileDescriptor()));

It seems you need to keep the ParcelFileDescriptor alive from garbage collection by putting it into a local field like this: 看来您需要通过将其放入如下本地字段中来使ParcelFileDescriptor保持不被垃圾回收:

private ParcelFileDescriptor descriptor;

And do this: 并执行以下操作:

descriptor = getContentResolver().openFileDescriptor(data.getData(), "w");
writeLargeDataToStream(new FileOutputStream(descriptor.getFileDescriptor()));

When you've done using that, let garbage collector know it's collectible by using: 使用完之后,请使用以下命令让垃圾收集器知道它是可收集的:

descriptor = null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM