【问题标题】:Android ContentProvider freeze after writing 65535 bytes写入 65535 字节后,Android ContentProvider 冻结
【发布时间】:2015-05-08 18:10:05
【问题描述】:

我正在使用具有被覆盖的 openFile 方法的内容提供程序:

@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    ParcelFileDescriptor[] pipe = null;
    InputStream in = null;

    try {
        pipe = ParcelFileDescriptor.createPipe();
        String path = uri.getPath();

        in = new FileInputStream(new File(path));
        PipeFeederThread p = new PipeFeederThread(in, new AutoCloseOutputStream(pipe[1]));
        p.start();

    /*catches.... */

    return (pipe[0]);
}

PipeFeederThread 包含通常的块写入过程:

byte[] buf = new byte[8192];
int len;            
try {
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
        Log.i("WRITE","Writing...");
    }
    Log.i("WRITE","Finished");
    in.close();
    out.flush();
    out.close();
} catch (Exception e) {

}

但是,当我尝试将我的提供程序与画廊一样的ACTION_VIEW 意图一起使用时,提供程序仅将 65535 个字节写入“输出”(日志仅显示 8 次),然后什么也没有发生。 当我们达到 65535“限制”时,我创建了一个 counter int 来中断 while 循环,但随后它会传输一个损坏的文件。

【问题讨论】:

  • Well.len 可以小于 8192。记录一下。并总结。
  • while ((len = in.read(buf)) > 0) { Log.i("WRITE","Writing..."); out.write(buf, 0, len); Log.i("WRITE",String.format("Written %d bytes!",len); } 它上升到 65536 (8192*8) 然后只出现“Writing...”并且我的应用程序挂在了 out.write
  • len 不能达到那么多,因为 buf 只有 8192。
  • 尝试使用小于 16 位 maxsize 的文件。
  • 你能给出更复杂的代码示例,我们可以测试吗?

标签: android android-contentprovider outputstream file-descriptor limits


【解决方案1】:

您应该使用ParcelFileDescriptor.open() 方法。像这样的:

private val File.assetFileDescriptor get() = AssetFileDescriptor(ParcelFileDescriptor.open(this, MODE_READ_ONLY), 0, length())

然后在你的代码中简单地做:

return in.assetFileDescriptor

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 2020-07-22
    • 2016-06-10
    • 1970-01-01
    • 2019-01-31
    相关资源
    最近更新 更多