Android 如何从意向过滤器获取文件路径(电子邮件附件/下载)

Android 如何从意向过滤器获取文件路径(电子邮件附件/下载),android,android-intent,intentfilter,Android,Android Intent,Intentfilter,我在清单中定义了以下意图过滤器,允许我打开从浏览器和电子邮件附件下载的.ips文件 <intent-filter android:icon='@drawable/ic_launcher' android:label="@string/quick_patcher_label" android:priority='1'> <action android:name=

我在清单中定义了以下意图过滤器,允许我打开从浏览器和电子邮件附件下载的.ips文件

<intent-filter
                android:icon='@drawable/ic_launcher'
                android:label="@string/quick_patcher_label"
                android:priority='1'>
            <action android:name="android.intent.action.VIEW" />
            <action android:name="android.intent.action.EDIT" />
            <action android:name="android.intent.action.PICK" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:mimeType="*/*" />
            <data android:pathPattern="*.ips" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data android:mimeType="*/*" android:scheme="http" android:host="*" android:pathPattern=".*\\.ips" />
            <data android:mimeType="*/*" android:scheme="https" android:host="*" android:pathPattern=".*\\.ips" />
        </intent-filter>
如何从活动中检索使用此意图筛选器打开的文件的文件路径


可能没有“文件路径”。您可以使用
getIntent().getData()
获取数据的
Uri
。然后,使用
ContentResolver
openInputStream()
获取
InputStream
对于来自电子邮件的
Uri
的数据,您可能得到的是额外的文本,而不是流。所以流输出为空

电子邮件永远不会作为文件存在。您可以将它们保存到文件中。但是,它们不再是电子邮件


所以,下一次当你被问到:“什么时候电子邮件不是电子邮件?”时,你会知道答案,提问者会向你的智慧低头,要求成为你的门徒。但我离题了…

我明白了,你有没有机会用代码示例编辑你的答案?谢谢,如果我这样做了,得到了一个输入流,那么我怎么才能得到文件路径?@Linkandzelda:你没有得到文件路径,因为没有文件路径。您可以使用
InputStream
获取数据。如果您只能使用文件路径,请将您的应用程序限制为
文件
方案。那么您是说我从web浏览器下载到设备上的文件没有文件路径?@Linkandzelda:可能有,也可能没有,即使有,您也可能没有访问该文件路径的权限。保证您可以访问
Uri
(否则,调用您的应用程序将被破坏)。但这很容易成为
内容://
Uri
,其中
内容提供者
从内部存储器上的文件(您无法访问)、从数据库
BLOB
列(非文件)、从资产(非文件)提供数据,等等。编写应用程序只处理
Uri
和生成的
InputStream
public String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}