我要content开头的uri
时间: 2023-08-08 12:13:26 浏览: 47
可以使用以下代码获取 sdcard 目录下文件的 Content Uri:
```java
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/myFile.txt");
Uri uri = Uri.fromFile(file);
// 将File类型的Uri转换为Content类型的Uri
Uri contentUri = getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
// 在AndroidManifest.xml文件中添加以下内容:
// <application>
// <provider
// android:name="androidx.core.content.FileProvider"
// android:authorities="${applicationId}.provider"
// android:exported="false"
// android:grantUriPermissions="true">
// <meta-data
// android:name="android.support.FILE_PROVIDER_PATHS"
// android:resource="@xml/provider_paths" />
// </provider>
// </application>
// 在res/xml目录下创建名为provider_paths.xml的文件,文件内容如下:
// <paths>
// <external-path name="external_files" path="."/>
// </paths>
```
这将返回一个以 `content://` 开头的 Uri,该 Uri 可以用于读取或写入外部存储设备上的文件。请注意,将 File Uri 转换为 Content Uri 需要使用 FileProvider,需要在 AndroidManifest.xml 文件中添加相关配置。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)