19

内容提供者/解析器 API 提供了一种复杂但稳健的方式,使用 URI 和openInputStream()andopenOutputStream()方法在进程之间传输数据。自定义内容提供者能够openFile()使用自定义代码覆盖该方法,从而有效地将 URI 解析为Stream; 但是, 的方法签名openFile()具有ParcelFileDescriptor返回类型,并且不清楚如何为动态生成的内容生成正确的表示以从该方法返回。

从内容提供者返回内存映射的 InputStream?

现有代码库中是否有动态内容的实现ContentProvider.openFile()方法示例?如果没有,您能否建议这样做的源代码或流程?

4

2 回答 2

27

从总是很有帮助的 CommonsWare 中查看这个很棒的示例项目。它允许您创建一个 ParcelFileDescriptor 管道,其中一侧带有您想要的任何 InputStream,另一侧带有接收应用程序:

https://github.com/commonsguy/cw-omnibus/tree/master/ContentProvider/Pipe

关键部分是在以下位置创建管道openFile

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

    try {
      pipe=ParcelFileDescriptor.createPipe();
      AssetManager assets=getContext().getResources().getAssets();

      new TransferThread(assets.open(uri.getLastPathSegment()),
                       new AutoCloseOutputStream(pipe[1])).start();
    }
    catch (IOException e) {
      Log.e(getClass().getSimpleName(), "Exception opening pipe", e);
      throw new FileNotFoundException("Could not open pipe for: "
          + uri.toString());
    }

    return(pipe[0]);
  }

然后创建一个保持管道满的线程:

static class TransferThread extends Thread {
    InputStream in;
    OutputStream out;

    TransferThread(InputStream in, OutputStream out) {
        this.in = in;
        this.out = out;
    }

    @Override
    public void run() {
        byte[] buf = new byte[8192];
        int len;

        try {
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

            in.close();
            out.flush();
            out.close();
        } catch (IOException e) {
            Log.e(getClass().getSimpleName(),
                    "Exception transferring file", e);
        }
    }
}
于 2013-02-06T16:44:06.717 回答
2

MemoryFile 支持这一点,但公共 API 尚未最终确定。

于 2010-01-29T01:28:12.320 回答