【发布时间】: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