简体   繁体   English

ACTION_GET_CONTENT给出错误的路径

[英]ACTION_GET_CONTENT gives wrong path

I'm using ACTION_GET_CONTENT so that the user can select text files that the rest of my code can read and deal with. 我正在使用ACTION_GET_CONTENT,以便用户可以选择其余代码可以读取和处理的文本文件。

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("text/*");
startActivityForResult(Intent.createChooser(intent, "select data"), SELECT_DATA);

Above is my code so that the user can browse which works fine. 上面是我的代码,以便用户可以浏览正常运行的代码。

Uri DataUri = data.getData();
File FileUri = new File(DataUri.getPath());

If I convert DataUri or FileUri to a string after using getPath or getAbsolutePath, I get a completely wrong path. 如果在使用getPath或getAbsolutePath之后将DataUri或FileUri转换为字符串,则会得到完全错误的路径。

The path should be /storage/emulated/0/Documents/myFile but it gives me /document/primary:Documents/myFile. 路径应该是/ storage / emulated / 0 / Documents / myFile,但是它给了我/ document / primary:Documents / myFile。 I have no idea what this "primary:Documents" thing is. 我不知道这个“主要:文档”是什么。

The data from the intent itself already has the wrong path, any suggestions? 来自意图本身的数据已经具有错误的路径,有什么建议吗?

I'm using ACTION_GET_CONTENT so that the user can select text files 我正在使用ACTION_GET_CONTENT,以便用户可以选择文本文件

No, you are using ACTION_GET_CONTENT so that the user can select some content that has a MIME type of text/* . 不,您正在使用ACTION_GET_CONTENT以便用户可以选择MIME类型为text/*某些内容。 Where that content comes from is largely up to the user. 内容的来源很大程度上取决于用户。 It may or may not be a file on the filesystem, and if it is, is may or may not be a file that you could access yourself directly (eg, internal storage of the Dropbox app, files on removable storage on Android 4.4+ devices). 它可能是文件系统上的文件,也可能不是,如果是,则可能是您可以直接访问自己的文件(例如,Dropbox应用程序的内部存储,Android 4.4+设备上可移动存储中的文件) )。

The path should be /storage/emulated/0/Documents/myFile 路径应为/ storage / emulated / 0 / Documents / myFile

No, it should not. 不,不应该。

but it gives me /document/primary:Documents/myFile 但这给了我/ document / primary:Documents / myFile

What you get will vary by whatever activity handles the ACTION_GET_CONTENT request. 您得到的结果将随处理ACTION_GET_CONTENT请求的活动而变化。 What that activity is will depend on what the user has installed that supports ACTION_GET_CONTENT and what the user chooses out of that list of installed stuff. 该活动将取决于用户所安装的支持ACTION_GET_CONTENT内容以及用户从该已安装内容列表中选择的内容。

Usually, you will get a content Uri (ie, getScheme() on Uri will return content ), as the Uri will point to data being served by a ContentProvider . 通常情况下,你会得到一个content Uri (即getScheme()Uri将返回content ),作为Uri将指向由所服务的数据ContentProvider

The data from the intent itself already has the wrong path 来自意图本身的数据已经具有错误的路径

It has the correct path. 它具有正确的路径。 It is simply not a path on the filesystem. 它根本不是文件系统上的路径。

any suggestions? 有什么建议么?

Use a ContentResolver and openInputStream() to get an InputStream for the content represented by the content Uri . 使用ContentResolveropenInputStream()获取由content Uri表示的contentInputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM