具有“文件”方案的URI如何在Android中工作

问题描述 投票:1回答:1

我知道在Android中,我可以通过这种方式从磁盘读取文件:

Uri uri = Uri.parse("file:///data/data/com.example.mytest.mytest/test.txt");
InputStream stream = getContentResolver().openInputStream(uri);
int i;
while ((i = stream.read()) != -1) {
  Log.i("@@@", "znak: " + i);
}

但是,我不知道它是如何工作的。 Android中是否存在与“文件”方案兼容的某些内容提供程序?但是如何-我认为所有内容提供者的URI方案都必须具有“内容”方案?]

android android-contentprovider file-uri
1个回答
2
投票

ContentResolveropenInputStream()直接处理file:方案。这在the documentation for openInputStream()中涵盖。

此外,不对路径进行硬编码。您的openInputStream()在数百万个设备上是错误的:

  • 在Android 4.2+平板电脑和Android 5.0+手机上,用于访客和其他辅助帐户

  • 在Android M设备上,如果用户“采用”可移动存储并将应用移动到该移动存储,

© www.soinside.com 2019 - 2024. All rights reserved.