写入65535字节后,Android ContentProvider会冻结

时间:2015-05-08 18:10:06

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

我使用内容提供程序使用重写的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字节写入" out" (日志只显示8次)然后没有任何反应。 当我们达到65535"限制"时,我做了一个计数器来打破while循环。但随后它会传输一个损坏的文件。

1 个答案:

答案 0 :(得分:0)

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

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

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

return in.assetFileDescriptor