android pdf生成 路径,android – 如何从指向PDF文档的URI获取文件路径?
时间: 2024-02-23 09:02:10 浏览: 30
Android中的URI是用于标识和定位资源的抽象命名机制,它并不一定对应着文件系统中的路径。因此,从URI获取文件路径需要进行一些转换。
以下是从URI获取文件路径的一种方法:
```java
public String getFilePathFromUri(Context context, Uri uri) {
String filePath = "";
if (uri.getScheme().equals("content")) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
filePath = cursor.getString(column_index);
cursor.close();
} else if (uri.getScheme().equals("file")) {
filePath = uri.getPath();
}
return filePath;
}
```
在上述代码中,如果URI的Scheme是“content”,则需要从MediaStore中查询出文件路径;如果是“file”,则可以直接从URI中获取路径。
需要注意的是,该方法仅适用于从外部存储器中获取文件路径,如果URI对应的文件在应用内部存储器中,则需要使用其他方法。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)