Android-存储访问框架-Uri转换为本地文件 [英] Android - Storage Access Framework - Uri into local file
问题描述
我在我的应用程序中使用Storage Access Framework(SAF)
.为了扫描文件(照片或文档),我需要通过API发送本地文件路径.我已经设法正确设置了SAF
,现在当用户选择一个文件时,我得到了应该的Uri.
I am using Storage Access Framework(SAF)
in my app. In order to scan a file(photo or document) i need send a local file path trough an API. I've managed to setup the SAF
correctly, and now when a user chooses a file and i am getting a Uri as it supposed to be.
似乎Uri是云的密钥,而不是本地文件.
As it seems the Uri is a key for the cloud and not a local file.
Uri值如下所示:
content://com.android.providers.media.documents/document/image:11862
如何将这个Uri转换为本地文件?我应该从云端下载文件吗?我该怎么办?
How can i convert this Uri into a local file? Should i download the file from the cloud? How can i do it?
推荐答案
似乎Uri是云的密钥,而不是本地文件.
As it seems the Uri is a key for the cloud and not a local file.
Uri
是对内容的不透明引用.您无法知道数据在哪里,也不必关心.
The Uri
is an opaque reference to a piece of content. You have no way of knowing where the data is, nor should you care.
如何将这个Uri转换为本地文件?
How can i convert this Uri into a local file?
理想情况下,您不会.理想情况下,您可以使用支持InputStream
的某些库来扫描文件(照片或文档)".在这种情况下,您可以使用ContentResolver
和openInputStream()
将流传递到库中.
Ideally, you don't. Ideally, you "scan a file(photo or document)" using some library that supports an InputStream
. In that case, you use ContentResolver
and openInputStream()
to get the stream to pass to the library.
如果您的库不支持InputStream
作为数据源,则您需要自己使用openInputStream()
,并使用Java I/O在文件系统中将内容复制为文件,以便您通过到图书馆.
If your library does not support InputStream
as a data source, you will need to use openInputStream()
yourself, using Java I/O to make a copy of the content as a file in the filesystem, for you to pass to the library.
这篇关于Android-存储访问框架-Uri转换为本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!